#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll K[3030][3030], B[3030][3030];
ll R[3030], F[3030], P[101010];
ll ans[101010];
ll n,k;
ll run(ll p,ll d)
{
if(d <= F[p]) return B[p][d];
if(d == 0) return p;
return B[p][(d - F[p]) % R[p] + F[p]];
}
ll f(ll a,ll b)
{
if(a == b) return a;
if(a == 1){
if(K[b][1]){
ll r = k % (K[b][1] + 1);
if(r == 0) return a;
else if(r == 1) return b;
else return run(b, r-1);
}
else return run(b, k-1);
}
if(K[b][a]){
ll r = (k - K[1][a]) % (K[b][a] + 1);
if(r == 0) return a;
else if(r == 1) return b;
else return run(b, r-1);
}
else return run(b, k-1-K[1][a]);
}
int main()
{
scanf("%lld%lld",&n,&k);
if(n > 3000){
for(i=1;i<=n;i++){
scanf("%lld",P+i);
}
for(j=i=1;i<=n;i++){
chk[j] = 1;
j = P[j];
if(j == 1) break;
}
for(i=1;i<=n;i++) if(!chk[j]) ans[j] = n - i;
}
ll i,j,t;
for(i=1;i<=n;i++){
scanf("%lld",P+i);
}
for(i=1;i<=n;i++){
for(j=i,t=0;;j=P[j],t++){
if(K[i][j]) break;
K[i][j] = t;
B[i][t] = j;
}
R[i] = t-K[i][j];
F[i] = K[i][j];
}
ll cnt = 0;
if(n > 2000) return 0;
for(i=1;i<=n;i++) if(i == 1 || K[1][i]){
for(j=1;j<=n;j++){
ans[f(i,j)] ++;
}
cnt ++;
}
ans[run(1,k)] += n * (n - cnt);
for(i=1;i<=n;i++) printf("%lld\n",ans[i]);
return 0;
}
Compilation message
space_pirate.cpp: In function 'int main()':
space_pirate.cpp:47:7: error: 'i' was not declared in this scope
for(i=1;i<=n;i++){
^
space_pirate.cpp:51:7: error: 'j' was not declared in this scope
for(j=i=1;i<=n;i++){
^
space_pirate.cpp:51:9: error: 'i' was not declared in this scope
for(j=i=1;i<=n;i++){
^
space_pirate.cpp:52:4: error: 'chk' was not declared in this scope
chk[j] = 1;
^~~
space_pirate.cpp:57:7: error: 'i' was not declared in this scope
for(i=1;i<=n;i++) if(!chk[j]) ans[j] = n - i;
^
space_pirate.cpp:57:25: error: 'chk' was not declared in this scope
for(i=1;i<=n;i++) if(!chk[j]) ans[j] = n - i;
^~~
space_pirate.cpp:57:29: error: 'j' was not declared in this scope
for(i=1;i<=n;i++) if(!chk[j]) ans[j] = n - i;
^
space_pirate.cpp:44:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld",&n,&k);
~~~~~^~~~~~~~~~~~~~~~~~
space_pirate.cpp:63:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",P+i);
~~~~~^~~~~~~~~~~~