Pagini recente » Cod sursa (job #1768144) | Cod sursa (job #2744074) | Cod sursa (job #966223) | Cod sursa (job #1405546) | Cod sursa (job #1401058)
#include <fstream>
using namespace std;
typedef pair<int, int> Pair;
const int kMaxM = 7005;
ifstream fin("rsir.in");
ofstream fout("rsir.out");
Pair init;
int x, y, z, mod, n, mod1[kMaxM], mod2[kMaxM];
int64_t a, b;
void Advance(Pair &p) {
int aux = p.second;
p.second = mod1[p.first] + mod2[p.second];
if (p.second >= mod)
p.second -= mod;
p.first = aux;
}
int main() {
fin >> init.first >> init.second >> a >> b >> x >> y >> z >> mod >> n;
for (int i = 0; i < mod; ++i) {
mod1[i] = (a * i * i + x * i + z) % mod;
mod2[i] = (b * i * i + y * i) % mod;
}
init.first %= mod;
init.second %= mod;
if (n < 2) {
fout << (n ? init.second : init.first) << "\n";
return 0;
}
Pair it1 = init, it2 = init;
Advance(it1);
Advance(it2);
Advance(it2);
int pos = 1;
while (it1 != it2) {
if (pos == n) {
fout << it1.first << "\n";
return 0;
}
++pos;
Advance(it1);
Advance(it2);
Advance(it2);
}
n %= pos;
while (n--)
Advance(it1);
fout << it1.first << "\n";
return 0;
}