Cod sursa(job #1680676)

Utilizator rughibemBelcineanu Alexandru Ioan rughibem Data 8 aprilie 2016 22:49:14
Problema Rsir Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.7 kb
#include<cstdio>
#define MAXMOD 7005
FILE *f=fopen("rsir.in","r"), *g=fopen("rsir.out","w");

struct Pereche{ unsigned int T1, T2; } Pi, Pj;

unsigned int a, b, x, y, z, MOD, preT1[MAXMOD], preT2[MAXMOD];
long long int N;

Pereche Next( Pereche Q ){
Pereche R;
unsigned int T3;

    T3 = preT1[Q.T1] + preT2[Q.T2] + z;

    while( T3 >= MOD )
           T3 -= MOD;

    R.T1 = Q.T2;
    R.T2 = T3;

    return R;
}

bool Egal( Pereche Q, Pereche W ){

    if( Q.T1 == W.T1 && Q.T2 == W.T2 )
        return 1;
        return 0;

}

void Rezolvare(){
unsigned int i, j, repeat;

    i = 1;
    j = 2;

    while(1){

        if( i == N ){

            repeat = 0;
            break;

        }

        if( Egal(Pi,Pj) ){

            repeat = (N-i) % (j-i);
            break;

        }

        if( j-i > MOD*MOD ){

            Pj = Next(Pi);
            j = i+1;

        }

        Pi = Next(Pi); i++;
        Pj = Next(Next(Pj)); j+=2;

    }

    while( repeat > 0 ){

        Pi = Next(Pi);
        repeat--;

    }

    fprintf(g,"%d\n",Pi.T2);

}

void Initializare(){
unsigned int i;

    for(i=0;i<=MOD-1;i++){

        preT1[i] = ( i * i * a ) % MOD + ( i * x ) % MOD;
        if( preT1[i] >= MOD )
            preT1[i] -= MOD;

        preT2[i] = ( i * i * b ) % MOD + ( i * y ) % MOD;
        if( preT2[i] >= MOD )
            preT2[i] -= MOD;

    }

}

int main(){

    fscanf(f,"%d %d %d %d %d %d %d %d %lld\n",&Pi.T1,&Pi.T2,&a,&b,&x,&y,&z,&MOD,&N);

    Pi.T1 %= MOD;
    Pi.T2 %= MOD;

    Initializare();
    Pj = Next(Pi);

    if( N == 0 ){ fprintf(g,"%d\n",Pi.T1); }
    else
        Rezolvare();

return 0;
}