Cod sursa(job #170960)

Utilizator razvi9Jurca Razvan razvi9 Data 3 aprilie 2008 16:23:07
Problema Rsir Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#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;
	int i=0,j=2;
	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;
	}
	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;
}