Cod sursa(job #1678989)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 7 aprilie 2016 16:49:21
Problema Rsir Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

ifstream fin("rsir.in");
ofstream fout("rsir.out");

int n;
int A, B, X, Y, Z, M;

inline pair<int, int> getNextState(const pair<int, int> &state) {

	pair<int, int> ret;

	ret.second = (1LL * A * state.first * state.first + 1LL * B * state.second * state.second + 1LL * X * state.first + 1LL * Y * state.second + Z) % 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;

	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!