Cod sursa(job #1447395)

Utilizator DiClauDan Claudiu DiClau Data 4 iunie 2015 12:05:24
Problema Stramosi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include<stdio.h>
using namespace std;
const int N = 250005, M = 300005;
int mat[20][M];
int main ()
{
    FILE *in, *out;
    in = fopen ("stramosi.in", "r");
    out = fopen ("stramosi.out", "w");
    int n, m;
    fscanf (in, "%d%d", &n, &m);
    int i;
    for (i = 1; i <= n; i++)
        fscanf (in, "%d", &mat[0][i]);
    int j;
    int putere = 1, c = 0;
    while (putere <= n)
    {
        putere <<= 1;
        c++;
    }
    c--;
    for (i = 1; i <= c; i++)
        for (j = 0; j <= n; j++)
            mat[i][j] = mat[i-1][mat[i-1][j]];
    int p, q;
    for (i = 0; i <= c; i++)
    {
        for (j = 0; j <= n; j++)
            fprintf (out, "%d ", mat[i][j]);
        fprintf (out, "\n");
    }
    j = 0;
    for (i = 1; i <= m; i++)
    {
        fscanf (in, "%d%d", &q, &p);
        j = 0;
            while (p != 0)
            {
                if (p % 2 != 0)
                    q = mat[j][q];
                p = p / 2;
                j++;
            }
        fprintf (out, "%d\n", q);
    }
    return 0;
}