Cod sursa(job #736179)

Utilizator BarracudaFMI-Alex Dobrin Barracuda Data 17 aprilie 2012 23:57:49
Problema Rsir Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include<fstream>
#define dim  1<<14
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,ans;
long long n;
long  t0,t1;
void precalcul () {
	
	for(long i=0;i<M;i++) {
		
		A[i]=(((i*i)%M)*a +(x*i))%M;
		
		B[i]=(((i*i)%M)*b+(y*i) + z )%M;
	}
}
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;
	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);
	i=0;

	p2=p1;
	for(lungime=M*M+1;i<lungime;++i)
		p2=next(p2);
	for(;n>=0&&p2!=p1;--n,--lungime,p1=next(p1))
	if(!n)
		ans=p1.first;
	
	if(n){
		n%=lungime;
		while( n>0 ) {
			p1=next(p1);
			--n;
		}
		if(n==0)
			ans=p1.first;
	}
	g<<ans;
	return 0;
	
}