Cod sursa(job #1951236)

Utilizator Theodor1000Cristea Theodor Stefan Theodor1000 Data 3 aprilie 2017 15:01:13
Problema Rsir Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.33 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, ca[8192], cb[8192];

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

    return rez;
}

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 < M; ++i)
    {
        long long aa = 1LL * i * i * a + 1LL * i * x;
        if (aa >= 1LL * M) aa %= 1LL * M;

        long long bb = 1LL * i * i * b + 1LL * i * y + 1LL * z;
        if (bb >= 1LL * M) bb %= 1LL * M;

        ca[i] = (int)aa;
        cb[i] = (int)bb;
    }

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

    p1 = 0, p2 = 0, x11 = t0, x12 = t1, x21 = t0, x22 = t1;

    do
    {
        int aux;

        aux = nxt (x11, x12);
        x11 = x12;
        x12 = aux;
        ++p1;

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

    } while (!(x11 == x21 && x12 == x22));

    p1 = 0, x11 = t0, x12 = t1;

    do
    {
        int aux;

        aux = nxt (x11, x12);
        x11 = x12;
        x12 = aux;
        ++p1;

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

    } while (!(x11 == x21 && x12 == x22));

    int st = 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;
    }

    p2 = p1, x21 = x11, x22 = x12;

    do
    {
        int aux;

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

    } while (!(x11 == x21 && x12 == x22));

    int per = p2 - p1;

    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;
}