Cod sursa(job #1558262)

Utilizator geni950814Geni Geni geni950814 Data 28 decembrie 2015 22:18:24
Problema Rsir Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <fstream>

using namespace std;

int T0, T1, a, b, x, y, z, M, n;

struct State {
    int fst;
    int snd;

    bool operator==(const State& rhs) const {
        if(this->fst == rhs.fst && this->snd == rhs.snd) {
            return true;
        }
        return false;
    }

    bool operator!=(const State& rhs) const {
        if(this->fst == rhs.fst && this->snd == rhs.snd) {
            return false;
        }
        return false;
    }

}head, slow, quick;

void GoNext(State& state) {
    int p = state.snd;
    int pp = state.fst;
    state.fst = p;
    int newNode = a*pp^2 + b*p^2 + x*pp + y*p + z;
    state.snd = newNode%M;
}

int main() {
    
    ifstream f("rsir.in");
    ofstream g("rsir.out");

    f >> T0 >> T1 >> a >> b >> x >> y >> z >> M >> n;
    
    head.fst = slow.fst = quick.fst = T0;
    head.snd = slow.snd = quick.snd = T1;
    
    do {
        GoNext(slow);
        GoNext(quick);
        GoNext(quick);
    }while(slow != quick);

    int tail = 0;
    
    while(head != quick) {
        GoNext(head);
        GoNext(quick);
        tail++;
        if(n == tail) {
            g << head.snd;
            return 0;
        }
    }

    int cycle = 0;

    do{
        GoNext(quick);
        cycle++;
    } while(head != quick);

    n = (n - tail)%cycle;

    while(n>0) {
        GoNext(quick);
        n--;
    }

    g << quick.snd;

    return 0;
}