Pagini recente » Cod sursa (job #1153570) | Cod sursa (job #2617261) | Cod sursa (job #629732) | Cod sursa (job #143578) | Cod sursa (job #2844124)
#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;
}