#include <cstdio>
#include <cstring>
#include <vector>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef long long ll;
const int N = 3e3 + 50;
const int M = 1e5 + 500;
const int LOG = 62;
int dis[N][N], udem[N], kor[N][N];
int bio[M], cnt[N], P[M], klsk, cik[N];
vector < int > red;
int n;
ll k, fin[M];
void precompute(int x){
memset(bio, 0, sizeof(bio));
int cur = x, cdis = 0;
while(!bio[cur]){
kor[x][cdis] = cur;
bio[cur] = 1;
dis[x][cur] = cdis++;
cur = P[cur];
}
udem[x] = cur;
if(cur == x)
cik[x] = cdis;
}
int siz = 0;
int get(int a, int b){
if(a == b)
return a;
int nw = (a - b + siz) % siz + 1;
int koji = (k - a) % nw;
if(koji == 0)
return a;
return (b + koji - 1) % siz;
}
int main(){
memset(dis, -1, sizeof(dis));
scanf("%d%lld", &n, &k);
for(int i = 1;i <= n;i++)
scanf("%d", P + i);
if(n > 3000){
int cur = 1;
while(!bio[cur]){
red.PB(cur);
bio[cur] = 1;
cur = P[cur];
siz++;
}
fin[red[k % siz]] += (ll)(n - siz) * n;
for(int dif = - siz - 1;dif <= siz - 1;dif++){
int a = -dif, b = 0;
if(dif >= 0)
a = 0, b = dif;
for(;a < siz && b < siz;){
int cur = get(a, b), kor = 0;
for(int k = 17;k >= 0;k--){
if(a + kor + (1 << k) >= siz || b + kor + (1 << k) >= siz)
continue;
if(get(a + kor + (1 << k), b + kor + (1 << k)) == cur)
kor += (1 << k);
}
fin[red[cur]] += kor + 1;
a += kor + 1; b += kor + 1;
}
}
for(int i = 1;i <= n;i++){
if(!bio[i])
printf("%d\n", siz);
else
printf("%lld\n", fin[i]);
}
return 0;
}
for(int i = 1;i <= n;i++)
precompute(i);
int koji_kl = (k - dis[1][udem[1]]) % cik[udem[1]];
klsk = kor[udem[1]][koji_kl];
for(int a = 1;a <= n;a++){
for(int b = 1;b <= n;b++){
int ans = 0;
if(dis[1][a] == -1){
ans = klsk;
}
else if(dis[b][a] != -1){
int koji = (k - dis[1][a]) % (dis[b][a] + 1);
if(koji == 0)
ans = a;
else
ans = kor[b][koji - 1];
}
else{
int koji = (k - dis[1][a] - 1 - dis[b][udem[b]]) % cik[udem[b]];
ans = kor[udem[b]][koji];
}
cnt[ans]++;
}
}
for(int i = 1;i <= n;i++)
printf("%d\n", cnt[i]);
return 0;
}
Compilation message
space_pirate.cpp: In function 'int main()':
space_pirate.cpp:53:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
53 | scanf("%d%lld", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~~
space_pirate.cpp:55:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
55 | scanf("%d", P + i);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
37504 KB |
Output is correct |
2 |
Correct |
23 ms |
37504 KB |
Output is correct |
3 |
Correct |
24 ms |
37504 KB |
Output is correct |
4 |
Correct |
24 ms |
37504 KB |
Output is correct |
5 |
Correct |
23 ms |
37504 KB |
Output is correct |
6 |
Correct |
23 ms |
37504 KB |
Output is correct |
7 |
Correct |
24 ms |
37496 KB |
Output is correct |
8 |
Correct |
23 ms |
37504 KB |
Output is correct |
9 |
Correct |
24 ms |
37504 KB |
Output is correct |
10 |
Correct |
24 ms |
37504 KB |
Output is correct |
11 |
Correct |
23 ms |
37504 KB |
Output is correct |
12 |
Correct |
24 ms |
37504 KB |
Output is correct |
13 |
Correct |
23 ms |
37496 KB |
Output is correct |
14 |
Correct |
23 ms |
37504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
37504 KB |
Output is correct |
2 |
Correct |
23 ms |
37504 KB |
Output is correct |
3 |
Correct |
24 ms |
37504 KB |
Output is correct |
4 |
Correct |
24 ms |
37504 KB |
Output is correct |
5 |
Correct |
23 ms |
37504 KB |
Output is correct |
6 |
Correct |
23 ms |
37504 KB |
Output is correct |
7 |
Correct |
24 ms |
37496 KB |
Output is correct |
8 |
Correct |
23 ms |
37504 KB |
Output is correct |
9 |
Correct |
24 ms |
37504 KB |
Output is correct |
10 |
Correct |
24 ms |
37504 KB |
Output is correct |
11 |
Correct |
23 ms |
37504 KB |
Output is correct |
12 |
Correct |
24 ms |
37504 KB |
Output is correct |
13 |
Correct |
23 ms |
37496 KB |
Output is correct |
14 |
Correct |
23 ms |
37504 KB |
Output is correct |
15 |
Correct |
112 ms |
50064 KB |
Output is correct |
16 |
Correct |
106 ms |
49272 KB |
Output is correct |
17 |
Correct |
120 ms |
50296 KB |
Output is correct |
18 |
Correct |
1068 ms |
73080 KB |
Output is correct |
19 |
Correct |
687 ms |
66040 KB |
Output is correct |
20 |
Correct |
617 ms |
68344 KB |
Output is correct |
21 |
Correct |
967 ms |
65272 KB |
Output is correct |
22 |
Correct |
426 ms |
59640 KB |
Output is correct |
23 |
Correct |
845 ms |
64724 KB |
Output is correct |
24 |
Correct |
374 ms |
58104 KB |
Output is correct |
25 |
Correct |
95 ms |
49784 KB |
Output is correct |
26 |
Correct |
1134 ms |
73080 KB |
Output is correct |
27 |
Correct |
383 ms |
55800 KB |
Output is correct |
28 |
Correct |
467 ms |
60792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
38540 KB |
Output is correct |
2 |
Correct |
558 ms |
40048 KB |
Output is correct |
3 |
Correct |
266 ms |
39796 KB |
Output is correct |
4 |
Correct |
49 ms |
38912 KB |
Output is correct |
5 |
Correct |
565 ms |
40048 KB |
Output is correct |
6 |
Correct |
257 ms |
39796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
37504 KB |
Output is correct |
2 |
Correct |
23 ms |
37504 KB |
Output is correct |
3 |
Correct |
24 ms |
37504 KB |
Output is correct |
4 |
Correct |
24 ms |
37504 KB |
Output is correct |
5 |
Correct |
23 ms |
37504 KB |
Output is correct |
6 |
Correct |
23 ms |
37504 KB |
Output is correct |
7 |
Correct |
24 ms |
37496 KB |
Output is correct |
8 |
Correct |
23 ms |
37504 KB |
Output is correct |
9 |
Correct |
24 ms |
37504 KB |
Output is correct |
10 |
Correct |
24 ms |
37504 KB |
Output is correct |
11 |
Correct |
23 ms |
37504 KB |
Output is correct |
12 |
Correct |
24 ms |
37504 KB |
Output is correct |
13 |
Correct |
23 ms |
37496 KB |
Output is correct |
14 |
Correct |
23 ms |
37504 KB |
Output is correct |
15 |
Correct |
112 ms |
50064 KB |
Output is correct |
16 |
Correct |
106 ms |
49272 KB |
Output is correct |
17 |
Correct |
120 ms |
50296 KB |
Output is correct |
18 |
Correct |
1068 ms |
73080 KB |
Output is correct |
19 |
Correct |
687 ms |
66040 KB |
Output is correct |
20 |
Correct |
617 ms |
68344 KB |
Output is correct |
21 |
Correct |
967 ms |
65272 KB |
Output is correct |
22 |
Correct |
426 ms |
59640 KB |
Output is correct |
23 |
Correct |
845 ms |
64724 KB |
Output is correct |
24 |
Correct |
374 ms |
58104 KB |
Output is correct |
25 |
Correct |
95 ms |
49784 KB |
Output is correct |
26 |
Correct |
1134 ms |
73080 KB |
Output is correct |
27 |
Correct |
383 ms |
55800 KB |
Output is correct |
28 |
Correct |
467 ms |
60792 KB |
Output is correct |
29 |
Correct |
49 ms |
38540 KB |
Output is correct |
30 |
Correct |
558 ms |
40048 KB |
Output is correct |
31 |
Correct |
266 ms |
39796 KB |
Output is correct |
32 |
Correct |
49 ms |
38912 KB |
Output is correct |
33 |
Correct |
565 ms |
40048 KB |
Output is correct |
34 |
Correct |
257 ms |
39796 KB |
Output is correct |
35 |
Incorrect |
49 ms |
39152 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |