Cod sursa(job #227826)

Utilizator hasegandaniHasegan Daniel hasegandani Data 5 decembrie 2008 16:58:29
Problema Stramosi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include<stdio.h>
#include <string.h>

#define nmax 1<<18
FILE *f=fopen("stramosi.in","r"),*g=fopen("stramosi.out","w");  
int t[18][nmax];
#define DIM 8192

char buf[DIM];
int p=0;



inline void cit(int &x)
{
x=0;
while(buf[p++] < '0') if(p == DIM)
fread(buf,0,DIM,f), p=0;
while(buf[p] <= '9')
{
x=x*10+buf[p]-'0';
if(++p == DIM)
fread(buf,0,DIM,f), p=0;
}
}

int main()
{
    fread(buf,0,DIM,f), p=0;        
    int n,m;
    fscanf(f,"%d%d",&n,&m);
    for(int i=1;i<=n;++i)
//        fscanf(f,"%d",&t[0][i]);
cit(t[0][i]);
    for (int i=1;(1<<i) <= n;++i)
    for (int j=1;j<=n;++j)
    t[i][j] = t[i-1][t[i-1][j]];
    for (int i=1;i<=m;++i)
    {
        int a,b;
        //fscanf(f,"%d%d",&a,&b);
        cit(a);cit(b);
        #define W(i) if ((1<<i)&b) a = t[i][a];
        W(0)W(1)W(2)W(3)W(4)W(5)W(6)W(7)W(8)W(9)W(10)W(11)W(12)W(13)W(14)W(15)W(16)W(17)W(18)
        fprintf(g,"%d\n",a);
    }
    return 0;
}