Cod sursa(job #1661830)

Utilizator alexpetrescuAlexandru Petrescu alexpetrescu Data 24 martie 2016 11:04:16
Problema Rsir Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <cstdio>
#define MAXM 7000
struct mycreation{
    int x, y;
    bool operator!=(mycreation a){
        return (a.x!=x)||(a.y!=y);
    }
    mycreation operator=(mycreation a){
        x=a.x;
        y=a.y;
        return a;
    }
}aux;
int mod, prim[MAXM], secund[MAXM];
inline void precalc(int a, int b, int x, int y, int z){
    for(int i=0; i<mod; i++){
        prim[i]=(1LL*a*i*i+x*i+z)%mod;
        secund[i]=(1LL*b*i*i+y*i)%mod;
    }
}
inline mycreation f(mycreation a){
    aux.x=a.y;
    aux.y=prim[a.x]+secund[a.y];
    if(aux.y>=mod){
        aux.y-=mod;
    }
    return aux;
}
int main(){
    int a, b, x, y, z, poz, lam, mu, i;
    long long n;
    mycreation tortoise, hare, x0;
    FILE *fin, *fout;
    fin=fopen("rsir.in", "r");
    fout=fopen("rsir.out", "w");
    fscanf(fin, "%d%d%d%d%d%d%d%d%lld", &x0.x, &x0.y, &a, &b, &x, &y, &z, &mod, &n);
    x0.x%=mod;
    x0.y%=mod;
    precalc(a%mod, b%mod, x%mod, y%mod, z%mod);
    tortoise=f(x0);
    hare=f(f(x0));
    while(tortoise!=hare){
        tortoise=f(tortoise);
        hare=f(f(hare));
    }
    mu=0;
    tortoise=x0;
    while(tortoise!=hare){
        tortoise=f(tortoise);
        hare=f(hare);
        mu++;
    }
    lam=1;
    hare=f(hare);
    while(tortoise!=hare){
        hare=f(hare);
        lam++;
    }
    if(n<=mu){
        poz=n;
    }else{
        poz=(n-mu)%lam+mu;
    }
    hare=x0;
    for(i=1; i<=poz; i++){
        hare=f(hare);
    }
    fprintf(fout, "%d\n", hare.x);
    fclose(fin);
    fclose(fout);
    return 0;
}