Pagini recente » Cod sursa (job #2743126) | Cod sursa (job #341479) | Cod sursa (job #3184661) | Cod sursa (job #1353138) | Cod sursa (job #1639332)
#include <algorithm>
#include <fstream>
#include <iostream>
using namespace std;
int a, b, x, y, z, t0, t1, MOD;
inline pair<int, int> f(const pair<int, int> &p) {
return make_pair(p.second, (1LL * a * p.first * p.first + 1LL * b * p.second * p.second + x * p.first + y * p.second + z) % MOD);
}
int detect(const pair<int, int> &x0) {
pair<int, int> tortoise, hare;
tortoise = f(x0);
hare = f(f(x0));
while (tortoise != hare) {
tortoise = f(tortoise);
hare = f(f(hare));
}
int cycle = 1;
tortoise = x0;
hare = f(tortoise);
while (hare != tortoise) {
hare = f(hare);
cycle += 1;
}
return cycle;
}
int main() {
ifstream fin("rsir.in");
ofstream fout("rsir.out");
long long n;
fin >> t0 >> t1 >> a >> b >> x >> y >> z >> MOD >> n;
t0 = t0 % MOD; t1 = t1 % MOD;
int cycle = detect(make_pair(t0, t1));
pair<int, int> p(t0, t1); n = n % cycle;
for (int i = 1; i <= n; ++ i) {
p = f(p);
}
fout << p.first << "\n";
return 0;
}