Cod sursa(job #2059764)

Utilizator Ruxandra985Nanu Ruxandra Laura Ruxandra985 Data 7 noiembrie 2017 16:11:41
Problema Rsir Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <cstdio>

using namespace std;

int newterm (int t1,int t0,int a,int b,int x,int y,int z,int m){
    return (((t0*t0)%m*a)%m + ((t1*t1)%m*b)%m + (t0*x)%m + (t1*y)%m + z%m) %m;
}
int main()
{
    FILE *fin=fopen ("rsir.in","r");
    FILE *fout=fopen ("rsir.out","w");
    int t0,t1,a,b,x,y,z,m,ta1,ta2,tc1,tc2,ok,p1,p2,aux,lc,i,ic;
    long long n;
    fscanf (fin,"%d%d%d%d%d%d%d%d%lld",&t0,&t1,&a,&b,&x,&y,&z,&m,&n);
    /// iau o testoasa si un iepure care pleaca amandoi de la inc
    /// pt a det lungimea ciclului
    tc1=tc2=t1;
    ta1=ta2=t0;
    ok=0;
    p1=p2=1;
    while (tc1!=tc2 || ta1!=ta2 || ok==0){
        ok=1;
        aux=tc1;
        tc1=newterm (tc1,ta1,a,b,x,y,z,m);
        ta1=aux;
        aux=tc1;
        tc1=newterm (tc1,ta1,a,b,x,y,z,m);
        ta1=aux;
        p1+=2;
        aux=tc2;
        tc2=newterm (tc2,ta2,a,b,x,y,z,m);
        ta2=aux;
        p2++;
    }
    lc=p1-p2;
    tc1=t1;
    ta1=t0;
    for (i=1;i<=lc;i++){
        aux=tc1;
        tc1=newterm (tc1,ta1,a,b,x,y,z,m);
        ta1=aux;
    }
    tc2=t1;
    ta2=t0;
    p2=0;
    while (tc2!=tc1 || ta2!=ta1){
        aux=tc1;
        tc1=newterm (tc1,ta1,a,b,x,y,z,m);
        ta1=aux;
        aux=tc2;
        tc2=newterm (tc2,ta2,a,b,x,y,z,m);
        ta2=aux;
        p2++;
    }
    p2--;
    ic=p2; // ic = inainte de ciclu
    if (n>ic)
        n=ic+(n-ic)%lc;
    tc1=t1;
    ta1=t0;
    for (i=2;i<=n;i++){
        aux=tc1;
        tc1=newterm (tc1,ta1,a,b,x,y,z,m);
        ta1=aux;
    }
    if (i==0)
        fprintf (fout,"%d",ta1);
    else
        fprintf (fout,"%d",tc1);
    return 0;
}