Cod sursa(job #736148)

Utilizator BarracudaFMI-Alex Dobrin Barracuda Data 17 aprilie 2012 23:06:37
Problema Rsir Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 kb
#include<fstream>
#define dim 7025
using namespace std;

ifstream f("rsir.in");
ofstream g("rsir.out");

typedef pair<int,int>cic;
int A[dim],B[dim];
int x,y,z,a,b,p1,p2,rr,inceput,i,lungime,M;
long long n,t0,t1;
void precalcul () {
	
	for(int i=0;i<M;i++) {
		
		A[i]=(i*i)%M;
		
		
	}
}
inline cic next(cic w) {
    int act = A[w.first]*a ;
    if(act >= M)
		act -= M;
	act+=A[w.second]*b;
	if(act >= M)
		act -= M;
	act+=(w.first*x);
	if(act >= M)
		act -= M;
	act+=w.second*y + z;
	if(act >= M)
		act -= M;
    return make_pair(w.second,act);
}
int main  () {
	
	f>>t0>>t1>>a>>b>>x>>y>>z>>M>>n;
	if(t0>=M)t0-=M;
	if(t1>=M)t1-=M;
	if(a>=M)a-=M;
	if(b>=M)b-=M;
	if(x>=M)x-=M;
	if(y>=M)y-=M;
	if(z>=M)z-=M;
	cic p1,p2,rr;
	
	precalcul();
	p1=make_pair(t0,t1);
	p2=next(p1);
	while (p1!=p2) {
		p1=next(p1);
		p2=next(next(p2));
	}
	inceput=0;
	p2=make_pair(t0,t1);
	while( p1!=p2  ) {
		p2=next(p2);
		inceput++;
	}
	
	lungime=1;
	p2=next(p2);
	while( p1!=p2 ) {
		p2=next(p2);
		++lungime;
	}
	if(n<=inceput) {
		rr=make_pair(t0,t1);
		for(i=1;i<=n;i++)
			rr=next(rr);
		g<<rr.first<<"\n";
		return 0;
	}
	else{
		n-=inceput;
		n%=lungime;
		for(;n;--n)
			p1=next(p1);
		g<<p1.first<<"\n";
		return 0;
	}
	return 0;
	
}