Cod sursa(job #1450767)

Utilizator heracleRadu Muntean heracle Data 14 iunie 2015 17:20:12
Problema Rsir Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <cstdio>

FILE* in=fopen("rsir.in","r");
FILE* out=fopen("rsir.out","w");

int t0,t1,a,b,x,y,z,M;
long long n;

int prec[7008],prec2[7008];
int precbis[7008],precbis2[7008];

void precal()
{
    long long act;
    for(int i=0; i<M; i++)
    {
        act=1LL*x*i;
        act+=1LL*a*i*i;
        act+=z;
        act%=M;
        prec[i]=act;

        act=1LL*y*i;
        act+=1LL*b*i*i;
        act%=M;
        precbis[i]=act;
    }
}

struct tipa
{
    int f,g;

    void operator =(const tipa &alta)
    {
        f=alta.f;
        g=alta.g;
    }

    void operator ++()
    {
        int act;
        act=prec[f] +precbis[g];

        if(act>=M)
            act-=M;
        f=g;
        g=act;
    }
} start;

 bool operator ==(const tipa &una,const tipa &alta)
{
    return (una.f==alta.f) & (una.g==alta.g);
}

void main2()
{
    if(n==0)
    {
        fprintf(out,"%d",start.f);
        return;
    }

    if(n<=60000000)
    {
        for(int i=1; i<n; i++)
            ++start;
        fprintf(out,"%d",start.g);
        return;
    }

    tipa iep,ste;

    ste=start;
    iep=start;
    ++iep;
    while(!(ste==iep) )
    {
        ++ste;
        ++iep;
        ++iep;
    }

    int l=1;

    ++iep;

    while(!(ste==iep))
    {
        ++iep;
        l++;
    }

    iep=start;
    ste=start;

    for(int i=1; i<=l; i++)
        ++iep;

    int bg=0;

    while(!(ste==iep))
    {
        ++iep;
        ++ste;
        bg++;
    }

    int my=(n-bg) %l;

    if(my==0)
        my=l;

    for(int i=1; i<my; i++)
        ++iep;

    fprintf(out,"%d",iep.g);

}


int main()
{
    fscanf(in,"%d%d",&t0,&t1);
    fscanf(in,"%d%d",&a,&b);
    fscanf(in,"%d%d%d",&x,&y,&z);
    fscanf(in,"%d%lld",&M,&n);

    precal();

    start.f=t0%M;
    start.g=t1%M;

    main2();

    return 0;
}