Pagini recente » Borderou de evaluare (job #2014906) | Cod sursa (job #38982) | Cod sursa (job #2884286) | Cod sursa (job #1644021) | Cod sursa (job #1821686)
#include <fstream>
#define LL long long
using namespace std;
ifstream cin ("rsir.in");
ofstream cout ("rsir.out");
const int MaxM = 7005;
LL t0, t1, a, b, x, y, z, M, n, tprev, tcurr, tprev2, tcurr2, CycleLength = 1;
LL A[MaxM], B[MaxM];
inline LL NextElement(LL tm2, LL tm1) {
return (A[tm2] + B[tm1] + z) % M;
}
int main() {
cin >> t0 >> t1 >> a >> b >> x >> y >> z >> M >> n;
for (int i = 1; i <= M; ++i) {
A[i] = a * i * i + x * i;
B[i] = b * i * i + y * i;
}
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;
tprev2 = tprev;
tcurr2 = tcurr;
do {
LL aux = tcurr2;
tcurr2 = NextElement(tprev2, tcurr2);
tprev2 = aux;
++CycleLength;
} while (tprev != t0 or tcurr != t1);
n %= CycleLength;
for (int i = 1; i < n; ++i) {
LL aux = tcurr;
tcurr = NextElement(tprev, tcurr);
tprev = aux;
}
cout << tcurr << '\n';
return 0;
}