Cod sursa(job #1401058)

Utilizator vladrochianVlad Rochian vladrochian Data 25 martie 2015 17:12:21
Problema Rsir Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#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;
}