Pagini recente » Cod sursa (job #209142) | Cod sursa (job #2405335) | Cod sursa (job #1593355) | Cod sursa (job #330959) | Cod sursa (job #527277)
Cod sursa(job #527277)
// 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 <= 19; 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 x = 1, c = 0;
while (x * 2 <= p)
{
x *= 2;
c++;
}
q = mat[c][q];
p -= x;
}
fprintf(fres, "%d\n", q);
}
fclose(fres);
fclose(f);
return 0;
}