Cod sursa(job #877801)

Utilizator AndreiRSStatescu Andrei Rares AndreiRS Data 13 februarie 2013 11:03:10
Problema Robotei Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <fstream>
using namespace std;

ifstream fi ("robotei.in");
ofstream fo ("robotei.out");

const int mmax = 666735, modmax = 1002;
int N, M, X, Y, modX, modY, offsetX, offsetY;
int F[mmax], R[modmax][modmax];

void cit ()
{
	fi >> N >> M >> X >> Y >> modX >> modY >> offsetX >> offsetY;
}

void pre ()
{
	int x, y, nrx, nry;
	for (x = 0; x < modX; x++)
	{
		for (y = 0; y < modY; y++)
		{
			nrx = (N - 1 - x) / modX + 1;
			nry = (N - 1 - y) / modY + 1;
			R[x][y] += nrx * nry;
		}
	}	
}

void rez ()
{
	int x, y, pozx, pozy, m, k;
	for (x = 0; x < modX; x++)
	{
		for (y = 0; y < modY; y++)
		{
			k = 0;
			
			pozx = x;
			pozy = y;
			
			for (m = 1; m <= M; m++)
			{
				pozx = (pozx * pozx + offsetX) % modX;
				pozy = (pozy * pozy + offsetY) % modY;
				
				if (pozx == X && pozy == Y)
				{
					k++;
				}
			}
			
			if (x == X && y == Y)
			{
				F[k] += R[x][y] - 1;
				F[k + 1] ++;
			}
			else
			{
				F[k] += R[x][y];
			}
		}
	}
}

void afi ()
{
	for (int m = 0; m <= M; m++)
	{
		if (F[m] == 0)
			continue;
		fo << m << ' ' << F[m] << '\n';
	}
}

int main ()
{
	cit ();
	pre ();
	rez ();
	afi ();
	
	return 0;
}