Cod sursa(job #1951056)

Utilizator Theodor1000Cristea Theodor Stefan Theodor1000 Data 3 aprilie 2017 13:40:29
Problema Rsir Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.42 kb
#include <cstdio>
#include <algorithm>

#define pii pair <int, int>
#define f first
#define s second

using namespace std;

int t0, t1, a, b, x, y, z, M;
int p1, p2, x11, x12, x21, x22, pas1, pas2, ca[18192], cb[18192];

int nxt (int tn2, int tn1)
{
    int rez = ca[tn2] + ca[tn1] + z;
    rez -= (rez >= M) * M;
    rez -= (rez >= M) * M;

    return rez;
}

void solve ()
{
    for (;;)
    {
        if (x11 == x21 && x12 == x22 && p2 > p1 + 1) return;

        int aux;

        if (pas1)
        {
            aux = nxt (x11, x12);
            x11 = x12;
            x12 = aux;
            ++p1;
        }

        aux = nxt (x21, x22);
        x21 = x22;
        x22 = aux;
        ++p2;

        if (pas2 > 0)
        {
            aux = nxt (x21, x22);
            x21 = x22;
            x22 = aux;
            ++p2;
        }
    }
}

int main ()
{
    freopen ("rsir.in", "r", stdin);
    freopen ("rsir.out", "w", stdout);

    scanf ("%d %d %d %d %d %d %d %d", &t0, &t1, &a, &b, &x, &y, &z, &M);

    if (t0 >= M) t0 %= M;
    if (t1 >= M) t1 %= M;
    if (a >= M) a %= M;
    if (b >= M) b %= M;
    if (x >= M) x %= M;
    if (y >= M) y %= M;
    if (z >= M) z %= M;

    for (int i = 0; i < 2 * M; ++i)
    {
        ca[i] = i * i;
        if (ca[i] >= M) ca[i] %= M;

        ca[i] *= a;
        if (ca[i] >= M) ca[i] %= M;

        ca[i] += x * i;
        if (ca[i] >= M) ca[i] %= M;

        cb[i] = i * i;
        if (cb[i] >= M) cb[i] %= M;

        cb[i] *= b;
        if (cb[i] >= M) cb[i] %= M;

        cb[i] += y * i;
        if (cb[i] >= M) cb[i] %= M;
    }

    long long n;
    scanf ("%lld", &n);

    p1 = 0, p2 = 0, x11 = t0, x12 = t1, x21 = t0, x22 = t1, pas1 = 1, pas2 = 2;
    solve ();

    p1 = 0, x11 = t0, x12 = t1, pas2 = 1;
    solve ();

    int st = p1;
    p2 = p1, x21 = x11, x22 = x12, pas1 = 0, pas2 = 1;
    solve ();

    int per = p2 - p1;
    if (n < 1LL * st)
    {
        x11 = t0, x12 = t1;
        for (int i = 1; i <= n; ++i)
        {
            int aux = nxt (x11, x12);
            x11 = x12;
            x12 = aux;
        }

        printf ("%d\n", x11);
        return 0;
    }

    n -= 1LL * st;
    n %= 1LL * per;

    for (int i = 1; i <= n; ++i)
    {
        int aux = nxt (x11, x12);
        x11 = x12;
        x12 = aux;
    }

    printf ("%d\n", x11);

    return 0;
}