#include using namespace std; using ll = long long; const int MAX_V = 100000; int V; vector G[MAX_V]; vector rG[MAX_V]; vector vs; bool used[MAX_V]; int cmp[MAX_V]; void add_edge(int from, int to) { G[from].push_back(to); rG[to].push_back(from); } void dfs(int v) { used[v] = true; for (int i = 0; i < G[v].size(); i++) { if (!used[G[v][i]]) dfs(G[v][i]); } vs.push_back(v); } void rdfs(int v, int k) { used[v] = true; cmp[v] = k; for (int i = 0; i < rG[v].size(); i++) { if (!used[rG[v][i]]) rdfs(rG[v][i], k); } } int scc() { memset(used, 0, sizeof(used)); vs.clear(); for (int v = 0; v < V; v++) { if (!used[v]) dfs(v); } memset(used, 0, sizeof(used)); int k = 0; for (int i = (int)vs.size() - 1; i >= 0; i--) { if (!used[vs[i]]) rdfs(vs[i], k++); } return k; } void dfs_end(int cur, ll rest, const vector< vector >& g, int n, const vector& p, vector& ans) { if (rest == 0) return; for (int prv : g[cur]) { ans[prv] = ans[cur] - n + prv + 1; dfs_end(prv, rest - 1, g, n, p, ans); } } void dfs_loop(int cur, ll rest, const vector< vector >& g, const int n, const vector& p, vector& ans, vector& memo, vector& loop_src) { if (cur < n) memo[cur] = rest; if (rest == 0) return; for (int prv : g[cur]) { ans[prv] = (cur == n ? 0 : ans[cur]) + prv + 1; if (cur != n) loop_src[prv] = loop_src[cur]; dfs_loop(prv, rest - 1, g, n, p, ans, memo, loop_src); } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; ll K; cin >> n >> K; vector p(n); for (int i = 0; i < n; ++i) { cin >> p[i]; --p[i]; } V = n; for (int i = 0; i < n; ++i) { if (p[i] + 1 == n) { continue; } add_edge(p[i], p[(i + p[i] + 1) % n]); } int sz = scc(); vector cnt(sz, 0); for (int i = 0; i < n; ++i) { ++cnt[cmp[i]]; } map loop_comp_ids; for (int i = 0; i < sz; ++i) { if (cnt[i] > 1) { int hoge = loop_comp_ids.size(); loop_comp_ids[i] = hoge; } } vector< vector > g(n + loop_comp_ids.size()); for (int i = 0; i < n; ++i) { int to; if (loop_comp_ids.find(cmp[i]) == loop_comp_ids.end()) { to = i; } else { to = n + loop_comp_ids[cmp[i]]; } for (int j : G[i]) { if (cmp[i] == cmp[j]) continue; int from; if (loop_comp_ids.find(cmp[j]) == loop_comp_ids.end()) { from = j; } else { from = n + loop_comp_ids[cmp[j]]; } g[from].push_back(to); } } vector ans(n, 0); ans[n - 1] = n * K; dfs_end(n - 1, K, g, n, p, ans); /* vector memo(n, -1); for (auto& foo : loop_comp_ids) { int loop_cmp = foo.first; vector loops; vector dict_loops(n, -1); vector loop_src(n, -1); for (int i = 0; i < n; ++i) { if (cmp[i] == loop_cmp) { loop_src[i] = i; memo[i] = K; if (loops.empty()) { int tar = i; while (tar != -1) { dict_loops[tar] = loops.size(); loops.push_back(tar); for (int nex : G[tar]) { if (nex == i) { tar = -1; break; } else { tar = nex; break; } } } } continue; } for (int j : G[i]) { if (cmp[j] == loop_cmp) { loop_src[i] = j; break; } } } int loop_size = loops.size(); vector sum_loops(2 * loop_size + 1, 0); for (int i = 0; i < loop_size; ++i) { sum_loops[i + 1] = sum_loops[i] + loops[i] + 1; } for (int i = 0; i < loop_size; ++i) { sum_loops[i + 1 + loop_size] = sum_loops[i + loop_size] + loops[i] + 1; } dfs_loop(n + foo.second, K, g, n, p, ans, memo, loop_src); for (int i = 0; i < n; ++i) { if (loop_src[i] == -1) continue; ll hoge = memo[i] / loop_size * sum_loops[loop_size]; memo[i] %= loop_size; hoge += sum_loops[dict_loops[loop_src[i]] + memo[i]] - sum_loops[dict_loops[loop_src[i]]]; ans[i] += hoge; } }*/ for (int i = 0; i < n; ++i) { cout << ans[p[i]] + i + 1 << "\n"; } return 0; }