Cod sursa(job #461575)

Utilizator GavrilaVladGavrila Vlad GavrilaVlad Data 7 iunie 2010 17:42:18
Problema Rsir Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 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; x%=m; y%=m; z%=m;    
    for(i=0; i<m; i++)
    {
        a1[i]=(1LL*i*i*a+i*x)%m;
        a2[i]=(1LL*i*i*b+i*y)%m;
    }
    for(i=1; i<=n && i<=m*m; i++)
        calc();
    if(i==n+1)
    {
        printf("%d\n", t0);
        return 0;
    }
    n-=i-1;
    to0=t0; to1=t1;
    calc();
    per=1;
    while(t0!=to0 && t1!=to1)
    {
        per++;
        calc();
    }
    n%=per;
    for(i=1; i<=n; i++)
        calc();
    printf("%d\n", t0);
    return 0;
}