Pagini recente » Cod sursa (job #599050) | Cod sursa (job #466255) | Cod sursa (job #931797) | Cod sursa (job #654356) | Cod sursa (job #527273)
Cod sursa(job #527273)
// 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, &m);
int mat[20][300000];// = {{ 0 }};
int i = 0, j = 0;
for (i = 1; i <= n; i++)
{
fscanf(f, "%d", &j);
mat[0][i] = j;
}
bool cont = true;
for (i = 1; i <= 18; 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;
}