Cod sursa(job #461572)

Utilizator GavrilaVladGavrila Vlad GavrilaVlad Data 7 iunie 2010 17:34:09
Problema Rsir Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <stdio.h>

using namespace std;

#define maxn 7010

long m, i, a, b, c, x, y, z, t0, t1, to0, to1, per;
long long n, a1[maxn], a2[maxn];

void calc()
{
    long t2;
    t2=(a1[t0]+a2[t1]+z)%m;
    t0=t1;
    t1=t2;
}

int main()
{
    freopen("rsir.in", "r", stdin);
    freopen("rsir.out", "w", stdout);
    scanf("%d%d%d%d%d%d%d%d%lld", &t0, &t1, &a, &b, &x, &y, &z, &m, &n);
    t0%=m; t1%=m; a%=m; b%=m; c%=m; x%=m; y%=m; z%=m;    
    for(i=0; i<m; i++)
    {
        a1[i]=1LL*i*i*a+i*x;
        a2[i]=1LL*i*i*b+i*y;
    }
    for(i=1; i<=n && i*i<=m; i++)
        calc();
    if(i==n+1)
    {
        printf("%d\n", t0);
        return 0;
    }
    n-=m*m;
    to0=t0; to1=t1;
    calc();
    per=1;
    while(t0!=to0 && t1!=to1)
    {
        per++;
        calc();
    }
    per--;
    n%=per;
    for(i=1; i<=n; i++)
        calc();
    printf("%d\n", t0);
    return 0;
}