Cod sursa(job #2833687)

Utilizator daria_pDaria Popescu daria_p Data 15 ianuarie 2022 15:10:14
Problema Rsir Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <fstream>

using namespace std;
ifstream fin("rsir.in");
ofstream fout("rsir.out");
long long n,i,j,m,t1,t2,t3,k,nr,a,b,c,x,y,z,ok,fr[8005][8005],cyc,v[100005],poz,len,x0,x1;
int main()
{
    fin >>t1>>t2>>a>>b>>x>>y>>z>>m>>n;
    t1=t1%m;
    t2=t2%m;
    fr[t1][t2]++;
    nr=2;
    k=1;
    v[1]=1;
    k=2;
    v[k]=t2;
    n++;
    while (ok==0)
    {
        t3=(long long)(a*t1*t1+b*t2*t2+x*t1+y*t2+z)%m;
        nr++;
        k++;
        v[k]=t3;
        if (fr[t2][t3]==0) fr[t2][t3]=nr;
        else
        {
            x0=t2;
            x1=t3;
            cyc=k-2;
            break;
        }
       //if (k==18) break;
        t1=t2%m;
        t2=t3%m;
    }
    len=2;
    while (ok==0)
    {
        t3=(long long)(a*t1*t1+b*t2*t2+x*t1+y*t2+z)%m;
        k++;
        v[k]=t3;
        len++;
        if (t2==x0 && t3==x1) break;
        t1=t2%m;
        t2=t3%m;
    }
    if (n<=cyc) fout <<v[n];
    else
    {
        if ((n-cyc)%len!=0) fout <<v[(n-cyc)%len+cyc];
        else fout <<v[len+cyc];
    }
    return 0;
}