#include <stdio.h>
#include <string.h>
#define N 100000
int solve(int *pp, int n, long long k) {
static int tt[N];
int i, t;
memset(tt, 0, n * sizeof *tt);
i = 0, t = 0;
while (!tt[i])
tt[i] = t++, i = pp[i];
k = (k - t) % (t - tt[i]);
while (k--)
i = pp[i];
return i;
}
int main() {
static int pp[N];
static long long tt[N], ans[N];
int n, i, j;
long long k, t, t_;
scanf("%d%lld", &n, &k);
for (i = 0; i < n; i++)
scanf("%d", &pp[i]), pp[i]--;
for (i = 0, t = k; !tt[i]; i = pp[i], t--)
tt[i] = t;
j = i, t_ = t % (tt[i] - t);
while (t_--)
j = pp[j];
for (i = 0; i < n; i++)
if (!tt[i])
ans[j] += n;
for (i = 0; i < n; i++)
if (tt[i]) {
int p = pp[i];
for (pp[i] = 0; pp[i] < n; pp[i]++)
ans[solve(pp, n, k)]++;
pp[i] = p;
}
for (i = 0; i < n; i++)
printf("%lld\n", ans[i]);
return 0;
}
Compilation message
space_pirate.c: In function 'main':
space_pirate.c:26:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | scanf("%d%lld", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~~~
space_pirate.c:28:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | scanf("%d", &pp[i]), pp[i]--;
| ^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
3 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
3 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
112 ms |
352 KB |
Output is correct |
16 |
Correct |
9 ms |
340 KB |
Output is correct |
17 |
Correct |
161 ms |
368 KB |
Output is correct |
18 |
Execution timed out |
2076 ms |
340 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2090 ms |
2452 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
3 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
112 ms |
352 KB |
Output is correct |
16 |
Correct |
9 ms |
340 KB |
Output is correct |
17 |
Correct |
161 ms |
368 KB |
Output is correct |
18 |
Execution timed out |
2076 ms |
340 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |