Cod sursa(job #134968)

Utilizator anna_bozianuBozianu Ana anna_bozianu Data 12 februarie 2008 19:05:39
Problema Rsir Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include<stdio.h>
long long int t0,t1,a,b,x,y,z,m,t2,n,i,p2,f0[7000],f1[7000],poz[7000][16],ppoz,per;
int main()
{
	FILE *f,*g;f=fopen("rsir.in","r");g=fopen("rsir.out","w");
	fscanf(f,"%lld%lld",&t0,&t1);
	fscanf(f,"%lld%lld",&a,&b);
	fscanf(f,"%lld%lld%lld",&x,&y,&z);
	fscanf(f,"%lld",&m);
	t0=t0%m;t1=t1%m;a=a%m;b=b%m;x=x%m;y=y%m;z=z%m;
	for(i=0;i<m;i++){p2=(i*i)%m;f0[i]=(a*p2+x*i+z)%m;f1[i]=(b*p2+y*i)%m;}
	while(!per)
	{ t2=f0[t0]+f1[t1];if(t2>=m)t2-=m;t0=t1;t1=t2;ppoz++;
	  if(t1<16)
	  { if(poz[t0][t1])per=ppoz-poz[t0][t1];
	    else poz[t0][t1]=ppoz;
	  }
	}
	fscanf(f,"%lld",&n);
	if(n>ppoz)
	{ n=n-ppoz;
	  n=n%per;
	  for(i=0;i<n;i++)
	  {t2=f0[t0]+f1[t1];if(t2>=m)t2-=m;t0=t1;t1=t2;}
	  fprintf(g,"%lld\n",t0);
	  fcloseall();
	  return 0;
	}
	else for(;;);
}