Cod sursa(job #170983)

Utilizator razvi9Jurca Razvan razvi9 Data 3 aprilie 2008 16:46:25
Problema Rsir Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include<cstdio>
#include<vector>
#define f first
#define s second
using namespace std;
int t0,t1,m,x,y,z,a,b,A[7001],B[7001],k,i,j,l;
long long n;
pair<int,int> ti,tj;
//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
pair<int,int> f(pair<int,int> t)
{
	pair<int,int> res;
	res.f=t.s;
	res.s=(A[t.f]+B[t.s])%m;
	return res;
}
int main()
{
	freopen("rsir.in","r",stdin);
	freopen("rsir.out","w",stdout);
	scanf("%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;
	ti.f=t0;
	ti.s=t1;
	for(i=0;i<m;i++)
	{
		k=(i*i)%m;
		A[i]=(k*a+i*x)%m;
		B[i]=(k*b+i*y+z)%m;
	}
	tj=f(ti);
	if(n==0) 
	{
		printf("%d\n",t0);
		goto end;
	}
	if(n==1)
	{
		printf("%d\n",t1);
		goto end;
	}

	for(l=1,n--;n>1 && ti!=tj;ti=f(ti),tj=f(f(tj)),n-=2,l++);
	if(n==0)
	{
		printf("%d\n",tj.f);
		goto end;
	}
	if(n==1)
	{
		printf("%d\n",tj.s);
		goto end;
	}
	n=n%l;
	while(n)
	{
		ti=f(ti);
		n--;
	}
	printf("%d\n",ti.f);
end:
	fclose(stdout);
	return 0;
}