Cod sursa(job #2073263)

Utilizator Andrei_CotorAndrei Cotor Andrei_Cotor Data 22 noiembrie 2017 21:24:29
Problema Rsir Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
#include<fstream>
using namespace std;
ifstream fi("rsir.in");
ofstream fo("rsir.out");
long long a,b,x,y,z,m,n,i,k;
pair<int,int> t,ct;
int A[7001],B[7001];

void build()
{
    int i;
    long long v;
    for(i=0; i<=m; i++)
    {
        v=a*(i*i)+x*i+z;
        A[i]=v%m;
        v=b*(i*i)+y*i;
        B[i]=v%m;
    }
}

int mod(int x)
{
    if(x>=m)
        x-=m;
    return x;
}

int f(pair<int,int> t)
{
    return mod(A[t.first]+B[t.second]+z);
}

pair<int,int> calc(pair<int,int> t, int n)
{
    int i;
    for(i=1; i<n; i++)
        t=make_pair(t.second,f(t));
    return t;
}

int main()
{
    fi>>t.first>>t.second>>a>>b>>x>>y>>z>>m>>n;
    t.first%=m;
    t.second%=m;
    build();
    if(n<=m*m)
    {
        fo<<calc(t,n).second<<"\n";
        fi.close();
        fo.close();
        return 0;
    }
    n=n-m*m;
    t=calc(t,m*m);
    ct=t;
    for(k=1; ; k++)
    {
        t=make_pair(t.second,f(t));
        if(t==ct)
            break;
    }
    fo<<calc(t,n%k).second<<"\n";
    fi.close();
    fo.close();
    return 0;
}