Cod sursa(job #444265)

Utilizator hadesgamesTache Alexandru hadesgames Data 19 aprilie 2010 21:11:43
Problema Rsir Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <cstdio>
FILE *in,*out;
int prep1[7500],prep2[7600],prep3[7500],prep0[7400];
int main()
{
	int n,m,x,y,z,a,b,i,t0,t1,ti0,ti1,aux;
	in=fopen("rsir.in","r");
	out=fopen("rsir.out","w");
	fscanf(in,"%d%d%d%d%d%d%d%d%d",&t0,&t1,&a,&b,&x,&y,&z,&m,&n);
	t0%=m;
	t1%=m;
	ti0=t0;
	ti1=t1;
	if (n==0||n==1)
	{
		if (n)
			fprintf(out,"%d\n",t1);
		else
			fprintf(out,"%d\n",t0);
		fclose(in);
		fclose(out);
	}
	for (i=1;i<=m;i++)
	{
		prep0[i]=i*i*a %m;
		prep1[i]=i*i*b%m;
		prep2[i]=i*x%m;
		prep3[i]=i*y%m;
	}
	for (i=1;i<n&&(t0!=ti0||ti1!=t1||i==1);i++)
	{
		aux=prep0[t0]+prep1[t1]+prep2[t0]+prep3[t1]+z;
	//	printf("%d \n",aux);
		while (aux>=m)
			aux-=m;
		t0=t1;
		t1=aux;
	}
	if (i==n)
	{
		fprintf(out,"%d\n",t1);
		fclose(in);
		fclose(out);
		return 0;
	}
	i--;
	n=n%i;
	t1=ti1;
	t0=ti0;
	for (i=1;i<=n;i++)
	{
	
		aux=prep0[t0]+prep1[t1]+prep2[t0]+prep3[t1]+z;
		while (aux>=m)
			aux-=m;
		t0=t1;
		t1=aux;
	}
	fprintf(out,"%d\n",t1);
	fclose(in);
	fclose(out);
	return 0;
}