Cod sursa(job #1219540)

Utilizator andreiiiiPopa Andrei andreiiii Data 14 august 2014 15:11:13
Problema Rsir Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <algorithm>
#include <cstdio>

using namespace std;

const int Mmax = 7005;

int Mod[5 * Mmax], Moda[Mmax], Modb[Mmax], Modx[Mmax], Mody[Mmax];

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

    int T0, T1, a, b, x, y, z, M;
    long long n;
    scanf("%d%d%d%d%d%d%d%d%lld", &T0, &T1, &a, &b, &x, &y, &z, &M, &n);

    T0 %= M;
    T1 %= M;

    for (int i = 1; i < 5 * M; ++i)
        Mod[i] = Mod[i - 1] + 1 == M ? 0: Mod[i - 1] + 1;

    for (int i = 0; i < M; ++i) {
        Moda[i] = 1LL * i * i * a % M;
        Modb[i] = 1LL * i * i * b % M;
        Modx[i] = i * x % M;
        Mody[i] = i * y % M;
    }

    ///

    int n10 = T0, n11 = T1, n20 = T1, n21 = Mod[Moda[T0] + Modb[T1] + Modx[T0] + Mody[T1] + z];

    int L = min(1LL * M * M + 4, n);
    for (int i = 1; i <= L; ++i) {
        int aux = Mod[Moda[n10] + Modb[n11] + Modx[n10] + Mody[n11] + z];
        n10 = n11;
        n11 = aux;
    }

    if (n <= M * M + 4) {
        printf("%d\n", n10);
        return 0;
    }

    n20 = n11;
    n21 = Mod[Moda[n10] + Modb[n11] + Modx[n10] + Mody[n11] + z];

    int Clen = 1;

    while (n10 != n20 || n11 != n21) {
        int aux = Mod[Moda[n20] + Modb[n21] + Modx[n20] + Mody[n21] + z];
        n20 = n21;
        n21 = aux;
        ++Clen;
    }

    int p = (n - M * M + 5) % Clen;

    for (int i = 1; i <= p; ++i) {
        int aux = Mod[Moda[n10] + Modb[n11] + Modx[n10] + Mody[n11] + z];
        n10 = n11;
        n11 = aux;
    }

    printf("%d\n", n10);
}