Cod sursa(job #417807)

Utilizator GheorgheMihaiMihai Gheorghe GheorgheMihai Data 14 martie 2010 20:53:20
Problema Rsir Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include<cstdio>
int v1[7005],v2[7005],ti0,ti1,t0,t1,a,b,x,y,z,m;
long long n;

inline void next(int &t0, int &t1)
{
	int t=v1[t0]+v2[t1]+z;
	while(t>=m)
		t-=m;
	t0=t1;
	t1=t;
}

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=t0%m;
	t1=t1%m;
	z=z%m;
	int i;
	for(i=0;i<m;++i)
	{
		v1[i]=((long long)a*i*i+x*i)%m;
		v2[i]=((long long)b*i*i+y*i)%m;
	}
	int t;
	for(i=1;i<=n && i<=m*m;i++)
	{
		t=v1[t0]+v2[t1]+z;
		while(t>=m)
			t-=m;
		t0=t1;
		t1=t;
	}
	i--;
	n-=i;
	if(!n)
	{
		printf("%d\n",t0);
		return 0;
	}
	ti0=t0;
	ti1=t1;
	t=v1[t0]+v2[t1]+z;
	while(t>=m)
		t-=m;
	t0=t1;
	t1=t;
	for(i=1;t0!=ti0 || t1!=ti1;i++)
	{
		t=v1[t0]+v2[t1]+z;
		while(t>=m)
			t-=m;
		t0=t1;
		t1=t;
	}
	n%=i;
	while(n--)
	{
		t=v1[t0]+v2[t1]+z;
		while(t>=m)
			t-=m;
		t0=t1;
		t1=t;
	}
	printf("%d\n",t0);
	return 0;
}