Cod sursa(job #736138)

Utilizator BarracudaFMI-Alex Dobrin Barracuda Data 17 aprilie 2012 22:32:05
Problema Rsir Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include<fstream>
#define dim 7200
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 () {
	long mod=M;
	for(int i=0;i<M;i++) {
		
		A[i]=((((i*i)%mod)*a)%mod +(x*i)%mod)%mod;
		
		B[i]=((((i*i)%mod)*b)%mod+(y*i)%mod +z)%mod;
		
	}
}
inline cic next(cic w) {
    int act = A[w.first] + B[w.second];
    if(act >= M)
		act -= M;
    return make_pair(w.second,act);
}
int main  () {
	
	f>>t0>>t1>>a>>b>>x>>y>>z>>M>>n;
	cic p1,p2,rr;
	precalcul();
	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;
	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;
	
	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;
	
}