#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <bitset>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
#include <iomanip>
#include <assert.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int,int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);}
#define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);}
#define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);}
#define es(x,e) (int e=fst[x];e;e=nxt[e])
#define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e])
#define SZ 666666
int n,a[SZ]; ll k;
bool vis[SZ];
ll cnt[SZ];
vector<int> cs[SZ]; int cn;
int main()
{
scanf("%d%lld",&n,&k);
for(int i=1;i<=n;++i)
scanf("%d",a+i);
for(int i=1;i<=n;++i) if(!vis[i])
{
vector<int> cy;
int j=i;
while(!vis[j])
cy.pb(j),vis[j]=1,j=a[j];
cs[++cn]=cy;
}
vector<int> s=cs[1];
int tg=s[k%s.size()];
cnt[tg]+=(ll)n*(n-int(s.size()));
for(int p=2;p<=cn;++p)
for(auto x:cs[p])
cnt[x]+=(ll)s.size();
int p=s.size();
//intact cycle
for(int i=1;i<=p;++i)
for(int j=0;j<p;++j)
{
int u=(k-j-1)/(p-i+1),
d=u*(p-i+1);
int R=min(k-1-d,ll(p-1));
for(int s=j;s<=R;++j)
assert((k-s-1)/(p-i+1)==u);
cnt[s[((k-1-d+i)%p+p)%p]]+=R-j+1;
j=R;
}
for(int i=1;i<=n;++i)
printf("%lld\n",cnt[i]);
}
Compilation message
space_pirate.cpp: In function 'int main()':
space_pirate.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%lld",&n,&k);
~~~~~^~~~~~~~~~~~~~~~
space_pirate.cpp:42:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",a+i);
~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
38 ms |
32376 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
38 ms |
32376 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
56 ms |
35584 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
38 ms |
32376 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |