Cod sursa(job #81076)

Utilizator pauldbPaul-Dan Baltescu pauldb Data 31 august 2007 12:17:27
Problema Rsir Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <stdio.h>

#define maxn 7010
#define ll long long

int t0,t1,v0,v1;
int w0,w1;
int a,b,x,y,z,mod,m;
ll n;
int A[maxn],B[maxn];

inline void next(int &t0,int &t1)
{
     int aux=t1;
     t1=A[t0]+B[t1];
     if (t1>=mod) t1-=mod;
     t1+=z;
     if (t1>=mod) t1-=mod;
     t0=aux;
}

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,&mod,&n);
    z%=mod;
    
    if (n==0) 
    {
         printf("%d\n",t0);
         return 0;
    }
    if (n==1) 
    {
         printf("%d\n",t1);
         return 0;
    }
    
    int i;
    for (i=0;i<mod;i++) A[i]=(1LL*i*i*a+x*i)%mod;
    for (i=0;i<mod;i++) B[i]=(1LL*i*i*b+y*i)%mod;
    
    v0=w0=t0;w1=v1=t1;
    
    do
    {            
        next(t0,t1);
        next(w0,w1);
        next(w0,w1);
    }
    while ((w0!=t0) || (w1!=t1));
    
    do
    {
          m++;
          next(t0,t1);
    }
    while ((w0!=t0) || (w1!=t1));
    n--;
    
    while ((v0!=t0) || (v1!=t1))
    {
          n--;
          next(v0,v1);
          if (n==0) 
          {
              printf("%d\n",v1);
              return 0;
          }
    }
    
    n%=m;
    for (i=1;i<=n;i++) next(v0,v1);
    printf("%d\n",v1);
    
    return 0;
}