Cod sursa(job #1865965)

Utilizator pepsiM4A1Ozturk Arif pepsiM4A1 Data 2 februarie 2017 13:34:57
Problema Rsir Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <cstdio>
#include <iostream>
using namespace std;
typedef pair<int,int> pairr;
pairr t;
int i,m,x,y,z,ct0[7021],ct1[7021];
long long n,a,b;
void gotonext(pairr &i)
{
    int j=i.second;
    i.second=(ct0[i.first]+ct1[i.second]);
    if (i.second>=m) i.second-=m;
    i.first=j;
}
int main()
{
    freopen ("rsir.in","r",stdin);
    freopen ("rsir.out","w",stdout);
    scanf("%d%d%lld%lld%d%d%d%d%lld",&t.first,&t.second,&a,&b,&x,&y,&z,&m,&n);
    t.first%=m;
    t.second%=m;
    for (i=0;i<m;i++)
    {
        ct0[i]=(a*i*i+x*i+z)%m;
        ct1[i]=(b*i*i+y*i)%m;
    }
    if (n==1)
    {
        printf("%d\n",t.second);
        return 0;
    }
    if (n==0)
    {
        printf("%d\n",t.first);
        return 0;
    }
    pairr p,q;
    q=t;
    p=q;
    int poz=1;
    gotonext(p);
    gotonext(q);
    gotonext(q);
    while(p!=q)
    {
        if (n==poz)
        {
            printf("%d\n",p.first);
            return 0;
        }
        ++poz;
        gotonext(p);
        gotonext(q);
        gotonext(q);
    }
    n%=poz;
    for (i=1; i<=n; i++) gotonext(p);
    printf("%d\n",p.first);
    return 0;
}