Cod sursa(job #2839943)

Utilizator chiriacandrei25Chiriac Andrei chiriacandrei25 Data 26 ianuarie 2022 19:27:48
Problema Rsir Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <fstream>

using namespace std;

long long n;
int t0, t1, a, b, x, y, z, m;

pair<int, int> iep, tes;

int nextEl(int t0, int t1) { // t1 - ultimul, t0 - penultimul
    return (1LL * a * t0 * t0 + 1LL * b * t1 * t1 + 1LL * x * t0 + 1LL * y * t1 + z) % m;
}

int main() {
    ifstream fin("rsir.in");
    ofstream fout("rsir.out");
    fin >> t0 >> t1 >> a >> b >> x >> y >> z >> m >> n;
    t0 %= m; t1 %= m;
    
    int t2 = nextEl(t0, t1), t3 = nextEl(t1, t2);
    int t = 1;
    iep = {t2, t3}; tes = {t1, t2};
    
    while(iep != tes) {
        t++;

        int x = nextEl(tes.first, tes.second);
        tes = {tes.second, x};

        int y = nextEl(iep.first, iep.second);
        int z = nextEl(iep.second, y);

        iep = {y, z};
    }

    int l_ciclu = t;

    // mut testoasa la inceput, iepurele sare doar 1 pas

    t = 0;
    tes = {t0, t1};

    while(iep != tes) {
        t++;

        int x = nextEl(tes.first, tes.second);
        tes = {tes.second, x};

        int y = nextEl(iep.first, iep.second);
        iep = {iep.second, y};
    }

    /*fout << t << " " << l_ciclu << "\n";
    fout << t0 << " " << t1 << " ";
    for(int i = 0; i < 100; i++) {
        int t2 = nextEl(t0, t1);
        fout << t2 << " ";
        t0 = t1; t1 = t2;
    }*/

    n = (n + 1 - t) % l_ciclu + t - 1;

    if(n == 0) {
        fout << t0;
    }
    else if(n == 1) {
        fout << t1;
    }
    else {
        for(int i = 1; i < n; i++) {
            int t2 = nextEl(t0, t1);
            t0 = t1; t1 = t2;
        }
        fout << t1;
    }
    return 0;
}