Cod sursa(job #2443749)

Utilizator StanCatalinStanCatalin StanCatalin Data 29 iulie 2019 13:49:54
Problema Rsir Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int dim = 7005;

int t0,t1,a,b,x,y,z,mod,n,p[dim],s[dim];

pair <int ,int > testoasa,iepure;

pair<int,int> next(pair<int , int> x)
{
    pair<int,int> a;
    a.first = x.second;
    a.second = p[x.first] + s[x.second];
    a.first %= mod;
    a.second %= mod;
    return a;
}

int main()
{
    in >> t0 >> t1 >> a >> b >> x >> y >> z >> mod >> n;
    t0 %= mod;
    t1 %= mod;
    a %= mod;
    b %= mod;
    x %= mod;
    y %= mod;
    z %= mod;
    for (int i=0; i<mod; i++)
    {
        p[i] = (1LL*a*i*i+x*i+z)%mod;
        s[i] = (1LL*b*i*i+y*i)%mod;
    }
    pair<int,int> start;
    start = make_pair(t0%mod,t1%mod);
    testoasa = next(start);
    iepure = next(next(start));
    while (testoasa != iepure)
    {
        testoasa = next(testoasa);
        iepure = next(next(iepure));
    }
    int len = 1;
    int omg = 0;
    testoasa = start;
    while (testoasa != iepure)
    {
        testoasa = next(testoasa);
        iepure = next(iepure);
        omg++;
    }
    iepure = next(iepure);
    while (testoasa != iepure)
    {
        iepure = next(iepure);
        len++;
    }
    if (n > omg)
    {
        n = (n-omg)%len + omg;
    }
    testoasa = start;
    for (int i=1; i<=n; i++)
    {
        testoasa = next(testoasa);
    }
    out << testoasa.first;
    return 0;
}