Cod sursa(job #2844124)

Utilizator mariaionescu2006Ionescu Maria mariaionescu2006 Data 3 februarie 2022 20:00:10
Problema Rsir Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("rsir.in");
ofstream fout ("rsir.out");
long long n;
int t0,t1,a,b,x,y,z,m;
int p[10000],s[10000];
pair<int,int>iep,tes;
int nextel(int t0,int t1)
{
    int sol=p[t0]+s[t1];
    if (sol>=m) sol=sol-m;
    return sol;
}
int main()
{
    fin >>t0>>t1>>a>>b>>x>>y>>z>>m>>n;
    t0=t0%m;t1=t1%m;
    long long c;
    for (int i=0;i<m;i++)
        {c=1LL*x*i;
         c=c+1LL*a*i*i;
         c=c+z;
         c=c%m;
         p[i]=c;
         c=1LL*y*i;
         c=c+1LL*b*i*i;
         c=c+z;
         c=c%m;
         s[i]=c;}
    int t=1;
    int t2=nextel(t0,t1),t3=nextel(t1,t2);
    iep={t2,t3};tes={t1,t2};
    while (iep!=tes)
          {t++;
           int x=nextel(tes.first,tes.second);
           tes={tes.second,x};
           int y=nextel(iep.first,iep.second);
           int z=nextel(iep.second,y);
           iep={y,z};}
    int l_ciclu=t;
    t=0;
    tes={t0,t1};
    while (iep!=tes)
          {t++;
           int x=nextel(tes.first,tes.second);
           tes={tes.second,x};
           int y=nextel(iep.first,iep.second);
           iep={iep.second,y};}
    n=(n+1-t)%l_ciclu+t-1;
    if(n==0) {fout << t0;}
    else if(n==1) {fout << t1;}
    else {for (int i=1;i<n;i++)
              {int t2=nextel(t0,t1);
               t0=t1;t1=t2;}
          fout <<t1;}
    return 0;
}