Pagini recente » Cod sursa (job #1841351) | Cod sursa (job #305543) | Cod sursa (job #793124) | Cod sursa (job #1442665) | Cod sursa (job #1679011)
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
ifstream fin("rsir.in");
ofstream fout("rsir.out");
long long n;
int A, B, X, Y, Z, M;
int mod1[7005], mod2[7005];
inline pair<int, int> getNextState(const pair<int, int> &state) {
pair<int, int> ret;
ret.second = mod1[state.first] + mod2[state.second];
if (ret.second >= M)
ret.second -= M;
ret.first = state.second;
return ret;
}
int main() {
pair<int, int> initialState;
fin >> initialState.first >> initialState.second >> A >> B >> X >> Y >> Z >> M >> n;
initialState.first %= M;
initialState.second %= M;
for (int i = 0; i < M; ++i) {
mod1[i] = (1LL * A * i * i + 1LL * X * i + Z) % M;
mod2[i] = (1LL * B * i * i + 1LL * Y * i) % M;
}
pair<int, int> stateA, stateB;
stateA = stateB = initialState;
for (int step = 1; step < n; ++step) {
stateA = getNextState(stateA);
stateB = getNextState(getNextState(stateB));
if (stateA != stateB)
continue;
//cycle found
int cycleLen = 1;
stateB = getNextState(stateB);
while (stateB != stateA) {
stateB = getNextState(stateB);
++cycleLen;
}
stateA = stateB = initialState;
for (int i = 1; i <= cycleLen; ++i)
stateB = getNextState(stateB);
//find articulation node
int queLen = 0;
while (stateA != stateB) {
++queLen;
stateA = getNextState(stateA);
stateB = getNextState(stateB);
}
n -= queLen;
n--;
n %= cycleLen;
for (int i = 1; i <= n; ++i)
stateA = getNextState(stateA);
fout << stateA.second << '\n';
return 0;
}
fout << stateA.second << '\n';
return 0;
}
//Trust me, I'm the Doctor!