Submission #519458

# Submission time Handle Problem Language Result Execution time Memory
519458 2022-01-26T11:53:50 Z amunduzbaev Space Pirate (JOI14_space_pirate) C++14
80 / 100
1413 ms 106276 KB
#include <bits/stdc++.h>
 
template <class T>
using Vec = std::vector<T>;
 
using ll = long long;
 
constexpr ll INF = std::numeric_limits<ll>::max();
 
int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
 
    int N;
    ll K;
    std::cin >> N >> K;
    Vec<int> A(N);
    for (auto &x: A) {
        std::cin >> x;
        x -= 1;
    }
 
    if (N <= 3000) {
        Vec<Vec<int>> goes(N);
        Vec<Vec<ll>> ord(N, Vec<ll>(N, INF));
        for (int u = 0; u < N; ++u) {
            goes[u].reserve(N);
            ord[u][u] = 0;
            goes[u].push_back(u);
            while (ord[u][A[goes[u].back()]] == INF) {
                const auto v = A[goes[u].back()];
                const auto k = (int) goes[u].size();
                ord[u][v] = k;
                goes[u].push_back(v);
            }
        }        
        
        const auto last = [&](const int u) {
            return goes[u].back();
        };
        const auto connects = [&](const int u) {
            return A[last(u)];
        };
 
        Vec<ll> loop(N);
        for (int i = 0; i < N; ++i) {
            loop[i] = ord[i][last(i)] - ord[i][connects(i)] + 1;
        }
        const auto kth = [&](const int u, const ll k) {
            if (k < (int) goes[u].size()) {
                return goes[u][k];
            }
            else {
                const auto rem = (k - (int) goes[u].size()) % loop[u];
                return goes[u][ord[u][connects(u)] + rem];
            }
        };
 
        Vec<int> ans(N);
        for (int a = 0; a < N; ++a) {
            for (int b = 0; b < N; ++b) {
                if (ord[0][a] >= K) {
                    ans[kth(0, K)] += 1;
                }
                else {
                    const auto rem = K - ord[0][a] - 1;
                    if (ord[b][a] >= rem) {
                        ans[kth(b, rem)] += 1;
                    }
                    else {
                        const auto idx = rem % (ord[b][a] + 1);
                        ans[kth(b, idx)] += 1;
                    }
                }
            }
        }
 
        for (const auto x: ans) {
            std::cout << x << '\n';
        }
    }
 
    else {
        Vec<int> belong(N, -1);
        Vec<Vec<int>> cycles;
 
        for (int src = 0; src < N; ++src) {
            if (belong[src] == -1) {
                const auto k = (int) cycles.size();
                cycles.push_back({ });
                int u = src;
                while (belong[u] == -1) {
                    belong[u] = k;
                    cycles[k].push_back(u);
                    u = A[u];
                }
            }
        }
 
        auto& cycle = cycles[0];
        const auto size = (int) cycle.size();
        Vec<ll> ans(N);
        ans[cycle[K % size]] += (ll) (N - size) * N;
        for (int i = 0; i < N; ++i) {
            if (belong[i] != 0) {
                ans[i] += std::min<ll>(size, K);
            }
        }
        for (int i = 0; i < size; ++i) {
            ans[cycle[i]] += 1;
        }
        for (int len = 2; len <= size; ++len) {
            const auto k = K % len;
            ans[cycle[k]] += len - k - 1;
            if (k != 0) {
                ans[cycle[size - len + k]] += k;
            }
        }
        for (int len = 2; len <= size; ++len) {
            for (int k = K % len; k < size; k += len) {
                const ll l = std::max(k, len - 1);
                const ll r = std::min(k + len - 1, size - 1);
                ans[cycle[k]] += r - l + 1;
            }
        }
 
        for (const auto x: ans) {
            std::cout << x << '\n';
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 444 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 436 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 440 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 388 KB Output is correct
13 Correct 1 ms 312 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 444 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 436 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 440 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 388 KB Output is correct
13 Correct 1 ms 312 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 237 ms 83888 KB Output is correct
16 Correct 209 ms 83172 KB Output is correct
17 Correct 243 ms 84148 KB Output is correct
18 Correct 1413 ms 106196 KB Output is correct
19 Correct 788 ms 99780 KB Output is correct
20 Correct 748 ms 102116 KB Output is correct
21 Correct 1254 ms 98912 KB Output is correct
22 Correct 613 ms 93444 KB Output is correct
23 Correct 956 ms 98236 KB Output is correct
24 Correct 472 ms 91768 KB Output is correct
25 Correct 212 ms 83532 KB Output is correct
26 Correct 1227 ms 106276 KB Output is correct
27 Correct 555 ms 89604 KB Output is correct
28 Correct 638 ms 94564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 3360 KB Output is correct
2 Correct 28 ms 3516 KB Output is correct
3 Correct 22 ms 3456 KB Output is correct
4 Correct 19 ms 3396 KB Output is correct
5 Correct 27 ms 3512 KB Output is correct
6 Correct 21 ms 3520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 444 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 436 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 440 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 388 KB Output is correct
13 Correct 1 ms 312 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 237 ms 83888 KB Output is correct
16 Correct 209 ms 83172 KB Output is correct
17 Correct 243 ms 84148 KB Output is correct
18 Correct 1413 ms 106196 KB Output is correct
19 Correct 788 ms 99780 KB Output is correct
20 Correct 748 ms 102116 KB Output is correct
21 Correct 1254 ms 98912 KB Output is correct
22 Correct 613 ms 93444 KB Output is correct
23 Correct 956 ms 98236 KB Output is correct
24 Correct 472 ms 91768 KB Output is correct
25 Correct 212 ms 83532 KB Output is correct
26 Correct 1227 ms 106276 KB Output is correct
27 Correct 555 ms 89604 KB Output is correct
28 Correct 638 ms 94564 KB Output is correct
29 Correct 16 ms 3360 KB Output is correct
30 Correct 28 ms 3516 KB Output is correct
31 Correct 22 ms 3456 KB Output is correct
32 Correct 19 ms 3396 KB Output is correct
33 Correct 27 ms 3512 KB Output is correct
34 Correct 21 ms 3520 KB Output is correct
35 Incorrect 20 ms 5624 KB Output isn't correct
36 Halted 0 ms 0 KB -