Cod sursa(job #733672)

Utilizator cnt_tstcont teste cnt_tst Data 12 aprilie 2012 18:58:35
Problema Rsir Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include<fstream>

using namespace std;

const char iname[]="rsir.in";
const char oname[]="rsir.out";
const int maxn=7005;

ifstream f(iname);
ofstream g(oname);

int m,i,j,a[maxn],b[maxn],x,y,u,v,p,q,z;

inline void next()
{
    int aux=a[p]+b[q]+z;
    while(aux>=m)
        aux-=m;
    p=q;q=aux;
}
long long n;
int main()
{
    f>>p>>q>>u>>v>>x>>y>>z>>m>>n;
    for(i=0;i<m;++i)
        a[i]=(1LL*u*i*i+1LL*x*i)%m,
        b[i]=(1LL*v*i*i+1LL*y*i)%m;

    for(i=1;i<=m*m&&i<=n;++i)
        next();
    x=p;
    y=q;
    if(i==n+1)
    {
        g<<x<<"\n";
        return 0;
    }
    n-=m*m;
    for(i=1;i<=n;++i)
    {
        next();
        if(p==x&&q==y)
            break;
    }
    n-=i;
    n%=i;
    for(i=1;i<=n;++i)
        next();
    g<<p<<"\n";
}