Cod sursa(job #2073244)

Utilizator Andrei_CotorAndrei Cotor Andrei_Cotor Data 22 noiembrie 2017 21:08:03
Problema Rsir Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 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 f(pair<int,int> t)
{
    return (((a*(t.first*t.first)%m)%m)+((b*(t.second*t.second)%m)%m)+((x*t.first)%m)+((y*t.second)%m)+z)%m;
}

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;
    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;
}