Cod sursa(job #1550503)

Utilizator lflorin29Florin Laiu lflorin29 Data 13 decembrie 2015 19:56:26
Problema Rsir Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <bits/stdc++.h>

using namespace std;
using lint = long long;

lint a, b, x, y;
int m, z;
lint n;

const int MAXM = 7007;
lint pre1[MAXM], pre2[MAXM];

struct Pair {
    int T0, T1;
    int nrsteps;
    Pair(int T0 = 0, int  T1 = 0) : nrsteps(0
                                                       ), T0(T0), T1(T1) {};

    bool operator == (const Pair& other) const {
        return this->T0 == other.T0 && this->T1 == other.T1;
    }

    void advance() {
        if(T0 >= m)
            T0 -= m;
        if(T1 >= m)
            T1 -= m;
        int  val = pre1[T0] + pre2[T1] + z;
        while(val>=m)
            val-=m;
        T0 = T1;
        T1 = val;
        nrsteps++;
    }
};

Pair init;


int main() {
    ifstream cin("rsir.in");
    ofstream cout("rsir.out");
    cin >> init.T0 >> init.T1 >> a >> b >> x >> y >> z >> m >> n;
    init.T0 %= m; init.T1 %= m;
    for(int i = 0; i <= m; ++i) {
        pre1[i] = (((a * i * i))%m + ((x * i)%m)) % m;
        pre2[i] = ((b * i * i) + (y * i)) % m;
    }

    Pair oth = init; Pair oth2 = init;

    oth.advance();
    while(!(init == oth)) {
         oth.advance();
        init.advance();
         oth.advance();
    }
    int cycle = oth.nrsteps - init.nrsteps ;
    n%=cycle;
    while(n--)
        init.advance();
    cout<<init.T1;}