Cod sursa(job #1736473)

Utilizator akaprosAna Kapros akapros Data 1 august 2016 19:56:28
Problema Rsir Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.23 kb
#include <bits/stdc++.h>
#define maxN 7000
#define ll long long
using namespace std;
int k, p, ans, t0, t1, a, b, x, y, z, sqr[maxN + 2], mod;
ll n;
void read()
{
    int i;
    freopen("rsir.in", "r", stdin);
    scanf("%d %d %d %d %d %d %d %d %lld", &t0, &t1, &a, &b, &x, &y, &z, &mod, &n);
    for (i = 1; i <= mod; ++ i)
        sqr[i] = (i * i) % mod;
}
void prec()
{
    int f2 = t1, s2 = t1, f1 = t0, s1 = t0, aux;
    do
    {
        aux = f2;
        f2 = (a * sqr[f1] + b * sqr[f2] + x * f1 + y * f2 + z) % mod;
        f1 = aux;
        aux = s2;
        s2 = (a * sqr[s1] + b * sqr[s2] + x * s1 + y * s2 + z) % mod;
        s1 = aux;
        aux = s2;
        s2 = (a * sqr[s1] + b * sqr[s2] + x * s1 + y * s2 + z) % mod;
        s1 = aux;
    }
    while (f2 != s2);
    int F1 = f1, F2 = f2;
    f2 = (a * sqr[f1] + b * sqr[f2] + x * f1 + y * f2 + z) % mod;
    f1 = F2;
    p = 1;
    while (f2 != F2 || f1 != F1)
    {
        ++ p;
        int aux = f2;
        f2 = (a * sqr[f1] + b * sqr[f2] + x * f1 + y * f2 + z) % mod;
        f1 = aux;
    }
    f1 = t0;
    f2 = t1;

    do
    {
        aux = f2;
        f2 = (a * sqr[f1] + b * sqr[f2] + x * f1 + y * f2 + z) % mod;
        f1 = aux;
        for (int i = 1; i <= p; ++ i)
        {
            aux = s2;
            s2 = (a * sqr[s1] + b * sqr[s2] + x * s1 + y * s2 + z) % mod;
            s1 = aux;
        }
    }
    while (f2 != s2);

    F1 = f1;
    F2 = f2;
    f1 = t0;
    f2 = t1;
    k = 2;
    while (f2 != F2 || f1 != F1)
    {
        ++ k;
        aux = f2;
        f2 = (a * sqr[f1] + b * sqr[f2] + x * f1 + y * f2 + z) % mod;
        f1 = aux;
    }
}
void solve()
{
    ll pos = n;
    prec();
    if (pos > k)
        pos = k - 1 + (pos - k + 1) % p;
    if (pos == 0)
        ans = t0;
    else if (pos == 1)
        ans = t1;
    else
    {
        while (-- pos)
        {
            int aux = t1;
            t1 = (a * sqr[t0] + b * sqr[t1] + x * t0 + y * t1 + z) % mod;
            t0 = aux;
        }
        ans = t1;
    }
}
void write()
{
    freopen("rsir.out", "w", stdout);
    printf("%d\n", ans);
}
int main()
{
    read();
    solve();
    write();
    return 0;
}