Pagini recente » Clasament 885900 | Cod sursa (job #1268939) | Cod sursa (job #2314830) | Cod sursa (job #2900796) | Cod sursa (job #1558319)
#include <fstream>
#include <iostream>
using namespace std;
int T0, T1, a, b, x, y, z, M, n;
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 GoNext(State& state) {
int p = state.snd;
int pp = state.fst;
state.fst = p;
long long newNode = a*(pp*pp) + b*(p*p) + 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%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 != quick);
int tail = 0;
while(head != quick) {
GoNext(head);
GoNext(quick);
tail++;
}
int cycle = 0;
do {
GoNext(quick);
cycle++;
}while(head != quick);
n = (n - tail)%cycle;
while(n>0) {
GoNext(quick);
n--;
}
g << quick.fst;
return 0;
}