Cod sursa(job #68673)

Utilizator azotlichidAdrian Vladu azotlichid Data 29 iunie 2007 00:09:33
Problema Rsir Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std;

#define MP make_pair
int T0, T1, a, b, x, y, z, M, cnt, len;
long long N;
pair<int, int> p1, p2;

pair<int, int> nxt(pair<int, int> T)
{
	return MP(T.second, (a * (T.first*T.first)%M + b * (T.second*T.second)%M + x*T.first + y*T.second + z)%M);
}

int main(void)
{
	freopen("rsir.in", "r", stdin);
	freopen("rsir.out", "w", stdout);
	scanf("%d %d %d %d %d %d %d %d %lld", &T0, &T1, &a, &b, &x, &y, &z, &M, &N);
	T0 %= M, T1 %= M;
	if (N == 0) { printf("%d\n", T0); return 0; }
	if (N == 1) { printf("%d\n", T1); return 0; }

	p1 = p2 = MP(T0, T1);
	cnt = 1;
	do
	{
		++ cnt;
		p1 = nxt(p1), p2 = nxt(nxt(p2));
		if (cnt == N) { printf("%d\n", p1.second); return 0; }
	} while (p1 != p2);

	len = 0;
	do
	{
		++ len;
		p1 = nxt(p1);
	} while (p1 != p2);

	(N -= cnt) %= len;
	for (; N; N--)
		p1 = nxt(p1);
	printf("%d\n", p1.second);
	return 0;
}