This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |