Cod sursa(job #527245)

Utilizator SilverMoonFeier Vlad SilverMoon Data 30 ianuarie 2011 23:19:47
Problema Stramosi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
// p010.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
//#include <cstdlib>
#include "stdio.h"


int main()
{
	FILE *f = fopen("stramosi.in", "rt");
	FILE *fres = fopen("stramosi.out", "wt");

	if (!f || !fres)
		return 0;

	int n = 0, m = 0;
	fscanf(f, "%d %d\n", &n, &m);

	int mat[20][250001];// = {{ 0 }};
	int i = 0, j = 0;

	//n = 250000; m = 300000;
	//for (i = 1; i <= n; i++) mat[0][i] = i;

	for (i = 1; i <= n; i++)
		fscanf(f, "%d", &mat[0][i]);

	bool cont = true;
	for (i = 1; cont; i++)
	{
		cont = false;
		for (j = 1; j <= n; j++)
		{
			mat[i][j] = mat[i - 1][mat[i - 1][j]];

			if (mat[i][j] != 0)
				cont = true;
		}
	}

	int q = 0, p = 0;
	for (i = 0; i < m; i++)
	{

		fscanf(f, "%d %d", &q, &p);
		//q = rand() % 18 + 1;
		//p = rand() % 250000 + 1;
		
		// al p-lea stramos al lui q
		/*while (p > 0 && q != 0)
		{
			int pow = 1, c = 0;
			while (pow * 2 <= p) 
			{ 
				pow *= 2; 
				c++; 
			}
			
			q = mat[c][q];

			p -= pow;
		}*/


		fprintf(fres, "%d\n", q);
	}

	fclose(fres);
	fclose(f);
	
	return 0;
}