Cod sursa(job #885801)
| Utilizator | Data | 22 februarie 2013 12:51:56 | |
|---|---|---|---|
| Problema | Stramosi | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva de probleme | Marime | 0.67 kb |
#include <fstream>
using namespace std;
int st[18][250001];
int n,m;
int main() {
int p,i,j,lg,k,q;
ifstream in("stramosi.in");
ofstream out("stramosi.out");
in>>n>>m;
p=0;
while (1<<p<n)
p++;
p--;
for (i=1; i<=n; i++)
in>>st[0][i];
for (i=1; i<=p; i++)
for (j=1; j<=n; j++)
st[i][j]=st[i-1][st[i-1][j]];
for (i=1; i<=m; i++) {
in>>k>>q;
while (q) {
p=0;
while (1<<p <= q)
p++;
p--;
k=st[p][k];
q-= 1<<p;
}
out<<k<<'\n';
}
out.close();
return 0;
}
