Pagini recente » Cod sursa (job #3212452) | Cod sursa (job #1081800) | Borderou de evaluare (job #2000301) | Cod sursa (job #921577) | Cod sursa (job #2410351)
#include <fstream>
#include <map>
using namespace std;
map<int, int> f[7001];
short v[7001 * 7001];
int main() {
ifstream cin ("rsir.in");
ofstream cout ("rsir.out");
long long t0, t1, a, b, x, y, z, n, t2, i, ok, m, n1, rep;
cin >> t0 >> t1 >> a >> b >> x >> y >> z >> m >> n;
a %= m;
b %= m;
x %= m;
y %= m;
z %= m;
t0 %= m;
t1 %= m;
i = 0;
ok = 0;
f[t0][t1] = 0;
v[0] = t0;
v[1] = t1;
n1 = 2;
while (i < n - 1 && (!ok)) {
i++;
t2 = 1ll * ((a * t0) % m * t0) % m + 1ll * ((b * t1) % m * t1) % m + (x * t0) % m + (y * t1) % m + z;
t2 %= m;
if (f[t1][t2] == 0) {
f[t1][t2] = i;
v[n1++] = t2;
}
else {
rep = i - f[t1][t2] + 1;
n -= f[t1][t2];
ok = 1;
}
if (ok == 0) {
t0 = t1;
t1 = t2;
}
}
if (ok == 1)
cout << v[f[t1][t2] + n % rep];
else
cout << t2;
return 0;
}