Cod sursa(job #2967980)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 20 ianuarie 2023 15:28:10
Problema Rsir Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <iostream>
#include <fstream>

using namespace std;

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

long long n;
int mod, t0, t1, a, b, x, y, z, start, loop = 1;
int v1[7005], v2[7005];

struct ura {
    int curr, urm;
    bool operator != (const ura &other) const {
        return curr != other.curr || urm != other.urm;
    }
}aux0, aux1;

ura nxt(ura x)
{
    return {x.urm, (v1[x.urm] + v2[x.curr]) % mod};
}

int main()
{
    fin >> t0 >> t1 >> a >> b >> x >> y >> z >> mod >> n;
    for(int i = 0; i < mod; i++)
    {
        v1[i] = ((((i * i) % mod) * b) % mod + (y * i) % mod + z) % mod;
        v2[i] = ((((i * i) % mod) * a) % mod + (x * i) % mod) % mod;
    }
    t0 = t0 % mod;
    t1 = t1 % mod;
    aux0 = nxt({t0, t1});
    aux1 = nxt(nxt({t0, t1}));
    while(aux0 != aux1)
    {
        aux0 = nxt(aux0);
        aux1 = nxt(nxt(aux1));
    }
    aux0 = {t0, t1};
    while(aux0 != aux1)
    {
        aux0 = nxt(aux0);
        aux1 = nxt(aux1);
        start++;
    }
    aux1 = nxt(aux0);
    while(aux0 != aux1)
    {
        aux1 = nxt(aux1);
        loop++;
    }
    aux0 = {t0, t1};
    if(n > start)
        n = start + (n - start) % loop;
    for(int i = 1; i <= n; i++)
        aux0 = nxt(aux0);
    fout << aux0.curr;
    return 0;
}