Cod sursa(job #1558367)

Utilizator geni950814Geni Geni geni950814 Data 29 decembrie 2015 00:32:47
Problema Rsir Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <fstream>
#include <iostream>
using namespace std;

typedef long long ll;

int T0, T1, a, b, x, y, z, M;
ll n;
int R0[7050];
int R1[7050];

struct State {
    int fst;
    int snd;

}head, slow, quick;
/*
bool operator==(const State& lhs, const State& rhs) {
    return lhs.fst == rhs.fst && lhs.snd == rhs.snd;
}

bool operator!=(const State& lhs, const State& rhs) {
    return !(lhs.fst == rhs.fst && lhs.snd == rhs.snd);
}
*/
void init() {
    for(int i = 0; i < M; i++) {
         long long patrat = i * i;
         R0[i] = (patrat * a + x * i) % M;
         R1[i] = (patrat * b + y * i) % M;
    }
}

void GoNext(State& state) {
    int p = state.snd;
    int pp = state.fst;
    state.fst = p;
    int newNode = R0[pp] + R1[p] + z;
    while(newNode >= M) {
        newNode -= M;
    }
    state.snd = newNode;
}

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

    f >> T0 >> T1 >> a >> b >> x >> y >> z >> M >> n;
    init(); 
    head.fst = slow.fst = quick.fst = T0%M;
    head.snd = slow.snd = quick.snd = T1%M;
    int i = 0; 
    do {
        if(i == n) {
            g << slow.fst;
            return 0;
        }
        GoNext(slow);
        GoNext(quick);
        GoNext(quick);
        i++;
    }while(slow.fst != quick.fst || slow.snd != quick.snd);

    int tail = 0;
    while(head.fst != quick.fst || head.snd != quick.snd) {
        GoNext(head);
        GoNext(quick);
        tail++;
    }
    int cycle = i;
    //do {
    //    GoNext(quick);
    //    cycle++;
    //}while(head.fst != quick.fst || head.snd != quick.snd);

    n = (n - tail)%cycle;

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

    g << quick.fst;

    return 0;
}