Cod sursa(job #68776)

Utilizator mariusdrgdragus marius mariusdrg Data 29 iunie 2007 13:46:12
Problema Rsir Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.84 kb
#include<stdio.h>


const int maxn = 8011;


int t1;
int t0;
int a;
int b;
int x;
int y;
int z;
long long n;
int i;
short m;
short t21;
short t11;
short t22;
short t12;
long long move1;
int s1[maxn];
int s2[maxn];

                      /*
inline int mod(int i)
{
        return i - (i / m) * m;
}

inline int sqr(short i)
{
        return mod(i * i);
}
                    */
void trans(short &p1,short &p2)
{
        short aux = p1;
        p1 = p2;
        p2 = s2[p2] + s1[aux] + z;
        while(p2 >= m) p2 -= m;
}


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;
        t21 = t0;
        t22 = t1;
        t11 = t0;
        --n;
        t12 = t1;
        for(i = 0;i <= m; ++i)
        {
                s1[i] = (a * ((i * i) % m) + x * i) % m;
                s2[i] = (b * ((i * i) % m) + y * i) % m;
        }
        while(t12 != t22 || t11 != t21 || move1 == 0)
        {
                trans(t11,t12);
                move1++;
                if (move1 == n)
                {
                        printf("%d\n",t12);
                        return 0;
                }
                trans(t21,t22);
                trans(t21,t22);
        }
        n -= move1;
        move1 = 0;
        while(t12 != t22 || t11 != t21 || move1 == 0)
        {

                trans(t11,t12);
                move1++;
        }
        n %= (long long)move1;
/*        if (n == 0)
        {
                printf("%d\n",t12);
                return 0;
        }                        */
        while(n)
        {
                trans(t11,t12);
                --n;
        }
        printf("%d\n",t12);
        return 0;
}