Pagini recente » Cod sursa (job #716231) | Cod sursa (job #724370) | Cod sursa (job #3139250) | Cod sursa (job #615465) | Cod sursa (job #2063367)
#include <cstdio>
using namespace std;
int v1[7001],v2[7001];
int main()
{
FILE *fin=fopen ("rsir.in","r");
FILE *fout=fopen ("rsir.out","w");
int t0,t1,a,b,x,y,z,m,ta1,ta2,tc1,tc2,ok,p2,aux,lc,i,ic;
long long n;
fscanf (fin,"%d%d%d%d%d%d%d%d%lld",&t0,&t1,&a,&b,&x,&y,&z,&m,&n);
t0%=m;
t1%=m;
a%=m;
b%=m;
x%=m;
y%=m;
z%=m;
for (i=0;i<m;i++){
v1[i]=(1LL*i*i*a + 1LL*i*x)%m;
v2[i]=(1LL*i*i*b + 1LL*i*y + 1LL*z)%m;
}
/// iau o testoasa si un iepure care pleaca amandoi de la inc
/// pt a det lungimea ciclului
tc1=tc2=t1;
ta1=ta2=t0;
ok=0;
p2=1;
while (tc1!=tc2 || ta1!=ta2 || ok==0){
ok=1;
ta1=v1[ta1]+v2[tc1];
if (ta1>=m)
ta1-=m;
tc1=v1[tc1]+v2[ta1];
if (tc1>=m)
tc1-=m;
//ta1=aux;
aux=tc2;
tc2=v1[ta2]+v2[tc2];
if (tc2>=m)
tc2-=m;
ta2=aux;
p2++;
}
//printf ("%d",p1-p2);
lc=p2-1;
tc1=t1;
ta1=t0;
for (i=1;i<=lc;i++){
aux=tc1;
tc1=v1[ta1]+v2[tc1];
if (tc1>=m)
tc1-=m;
ta1=aux;
}
tc2=t1;
ta2=t0;
p2=0;
while (tc2!=tc1 || ta2!=ta1){
aux=tc1;
tc1=v1[ta1]+v2[tc1];
if (tc1>=m)
tc1-=m;
ta1=aux;
aux=tc2;
tc2=v1[ta2]+v2[tc2];
if (tc2>=m)
tc2-=m;
ta2=aux;
p2++;
//printf ("%d %d %d %d\n",ta1,tc1,ta2,tc2);
}
p2--;
ic=p2; // ic = inainte de ciclu
if (n>ic)
n=ic+(n-ic)%lc;
tc1=t1;
ta1=t0;
for (i=2;i<=n;i++){
aux=tc1;
tc1=v1[ta1]+v2[tc1];
if (tc1>=m)
tc1-=m;
ta1=aux;
}
if (i==0)
fprintf (fout,"%d",ta1);
else
fprintf (fout,"%d",tc1);
return 0;
}