Cod sursa(job #3138443)

Utilizator YosifIosif Andrei Stefan Yosif Data 19 iunie 2023 16:26:02
Problema Rsir Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("rsir.in");
ofstream fout("rsir.out");

int t0, t1, a, b, x, y, z, mod;
long long n;
int aux1[7005], aux2[7005];
int Begin, Len;

#define Next(p) make_pair(p.second, aux1[p.first] + aux2[p.second] - (aux1[p.first] + aux2[p.second] >= mod ? mod : 0))

int main() {

    fin >> t0 >> t1;
    fin >> a >> b;
    fin >> x >> y >> z;
    fin >> mod >> n;

    t0 %= mod;
    t1 %= mod;

    for (int i = 0; i < mod; i++) {

        aux1[i] = (((a * i) % mod * i) % mod + (x * i) % mod) % mod;
        aux2[i] = (((b * i) % mod * i) % mod + (y * i) % mod + z) % mod;
    }

    pair<int, int> p1, p2;

    p1 = {t0, t1};
    p2 = {t0, t1};

    do {

        p1 = Next(p1);
        p2 = Next(p2), p2 = Next(p2);

    } while (p1 != p2);

    p1 = {t0, t1};

    while (p1 != p2) {

        p1 = Next(p1);
        p2 = Next(p2);
        Begin++;
    }

    p2 = Next(p2);
    Len++;
    while (p1 != p2) {

        p2 = Next(p2);
        Len++;
    }

    if (n > Begin)
        n = Begin + (n - Begin) % Len;

    p1 = {t0, t1};

    for (int i = 1; i <= n; i++)
        p1 = Next(p1);

    fout << p1.first;

    return 0;
}