Cod sursa(job #1400485)

Utilizator vladrochianVlad Rochian vladrochian Data 25 martie 2015 12:15:05
Problema Rsir Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <fstream>
using namespace std;

typedef pair<int, int> Pair;

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

Pair init;
int x, y, z, mod, n;
int64_t a, b;

void Adv1(Pair &p) {
	int aux = p.second;
	p.second = (x*p.first + y*p.second + z + a*p.first*p.first + b*p.second*p.second) % mod;
	p.first = aux;
}
void Adv2(Pair &p) {
	int aux1 = p.first, aux2 = p.second;
	p.first = (x*aux1 + y*aux2 + z + a*aux1*aux1 + b*aux2*aux2) % mod;
	p.second = (x*aux2 + y*p.first + z + a*aux2*aux2 + b*p.first*p.first) % mod;
}

int main() {
	fin >> init.first >> init.second >> a >> b >> x >> y >> z >> mod >> n;
	init.first %= mod;
	init.second %= mod;
	if (n < 2) {
		fout << (n ? init.second : init.first) << "\n";
		return 0;
	}
	Pair it1 = init, it2 = init;
	Adv1(it1);
	Adv2(it2);
	int pos = 1;
	while (it1 != it2) {
		if (pos == n) {
			fout << it1.first << "\n";
			return 0;
		}
		++pos;
		Adv1(it1);
		Adv2(it2);
	}
	n %= pos;
	while (n--)
		Adv1(it1);
	fout << it1.first << "\n";
	return 0;
}