Pagini recente » Cod sursa (job #1185796) | Cod sursa (job #1728088) | Cod sursa (job #2163391) | Cod sursa (job #1035208) | Cod sursa (job #170957)
Cod sursa(job #170957)
#include<cstdio>
int t0,t1,n,m,x,y,z,a,b;
class t
{
public:
int t1,t2;
void next()
{
int t3=a*t1*t1+b*t2*t2+x*t1+y*t2+z;
t3=t3%m;
t1=t2;
t2=t3;
}
void next2()
{
next();
next();
}
bool operator!=(t t1)
{
return !(t1.t1==this->t1 && t1.t2==this->t2);
}
};
//Tn = a * Tn-2^2 + b * Tn-1^2 + x * Tn-2 + y * Tn-1 + z
//T0, T1, a, b, x, y, z, M si n
int main()
{
freopen("rsir.in","r",stdin);
freopen("rsir.out","w",stdout);
scanf("%d %d %d %d %d %d %d %d %d",&t0,&t1,&a,&b,&x,&y,&z,&m,&n);
t0%=m;
t1%=m;
a%=m;
b%=m;
x%=m;
y%=m;
z%=m;
t ti,tj;
ti.t1=t0;
ti.t2=t1;
tj=ti;
if(n==0)
{
printf("%d\n",t0);
goto end;
}
if(n==1)
{
printf("%d\n",t1);
goto end;
}
int i=0,j=2;
tj.next2();
while(j+2<=n && ti!=tj)
{
ti.next();
tj.next2();
i++;
j+=2;
}
if(j==n)
{
printf("%d\n",tj.t1);
goto end;
}
if(j+1==n)
{
printf("%d\n",tj.t2);
goto end;
}
n=n-i;
n=n%(j-i);
while(n)
{
ti.next();
n--;
}
printf("%d\n",ti.t1);
end:
fclose(stdout);
return 0;
}