Pagini recente » Cod sursa (job #758155) | Cod sursa (job #2104825) | Cod sursa (job #105537) | Cod sursa (job #242064) | Cod sursa (job #1821641)
#include <fstream>
#define LL long long
using namespace std;
ifstream cin ("rsir.in");
ofstream cout ("rsir.out");
LL t0, t1, a, b, x, y, z, M, n, tprev, tcurr, CycleLength = 1;
inline LL NextElement(LL tm2, LL tm1) {
return (a * tm2 * tm2 % M + b * tm1 * tm1 % M + x * tm2 % M + y * tm1 % M + z) % M;
}
int main() {
cin >> t0 >> t1 >> a >> b >> x >> y >> z >> M >> n;
t0 %= M;
t1 %= M;
tprev = t0;
tcurr = t1;
for (CycleLength = 0; CycleLength < n and CycleLength < M * M; ++CycleLength) {
LL aux = tcurr;
tcurr = NextElement(tprev, tcurr);
tprev = aux;
}
if (CycleLength == n) {
cout << tprev << '\n';
return 0;
}
n %= CycleLength;
tprev = t0;
tcurr = t1;
do {
LL aux = tcurr;
tcurr = NextElement(tprev, tcurr);
tprev = aux;
} while (tprev != t0 or tcurr != t1);
cout << tcurr << '\n';
return 0;
}