Pagini recente » Cod sursa (job #2204871) | Cod sursa (job #207810) | Cod sursa (job #1601570) | Cod sursa (job #1339366) | Cod sursa (job #2839948)
#include <fstream>
using namespace std;
long long n;
int t0, t1, a, b, x, y, z, m;
int p[10000], s[10000];
pair<int, int> iep, tes;
int nextEl(int t0, int t1) { // t1 - ultimul, t0 - penultimul
int sol = p[t0] + s[t1];
if(sol >= m) {
sol -= m;
}
return sol;
//return (1LL * a * t0 * t0 + 1LL * b * t1 * t1 + 1LL * x * t0 + 1LL * y * t1 + z) % m;
}
int main() {
ifstream fin("rsir.in");
ofstream fout("rsir.out");
fin >> t0 >> t1 >> a >> b >> x >> y >> z >> m >> n;
t0 %= m; t1 %= m;
long long int curent;
for(int i = 0; i < m; ++i)
{
curent = 1LL * x * i;
curent += 1LL * a * i * i;
curent += z;
curent %= m;
p[i] = curent;
curent = 1LL * y * i;
curent += 1LL * b * i * i;
curent %= m;
s[i] = curent;
}
int t2 = nextEl(t0, t1), t3 = nextEl(t1, t2);
int t = 1;
iep = {t2, t3}; tes = {t1, t2};
while(iep != tes) {
t++;
int x = nextEl(tes.first, tes.second);
tes = {tes.second, x};
int y = nextEl(iep.first, iep.second);
int z = nextEl(iep.second, y);
iep = {y, z};
}
int l_ciclu = t;
// mut testoasa la inceput, iepurele sare doar 1 pas
t = 0;
tes = {t0, t1};
while(iep != tes) {
t++;
int x = nextEl(tes.first, tes.second);
tes = {tes.second, x};
int y = nextEl(iep.first, iep.second);
iep = {iep.second, y};
}
n = (n + 1 - t) % l_ciclu + t - 1;
if(n == 0) {
fout << t0;
}
else if(n == 1) {
fout << t1;
}
else {
for(int i = 1; i < n; i++) {
int t2 = nextEl(t0, t1);
t0 = t1; t1 = t2;
}
fout << t1;
}
return 0;
}