Official
E - Permute K times 2 Editorial
by
E - Permute K times 2 Editorial
by
MMNMM
頂点 頂点 頂点 からなり、 の 辺がある有向グラフを考えます。
このグラフにおいて、 から 回進んだ頂点を と呼ぶことにします(どの頂点についてもその頂点から出る辺は 本のみであるため、これは一意に定まります)。
求める答えは です(証明は省略しますが、 についての帰納法で示すのが平易だと思います)。
有向グラフはいくつかのサイクルに分けることができます。 それぞれのサイクルについて、 をそのサイクルの長さで割った余りを考えれば最終的な値を求めることができます。
サイクルの個数を として時間計算量は となります。
実装例は以下のようになります。
Copy
#include <atcoder/math>
#include <iostream>
#include <vector>
int main() {
using namespace std;
unsigned N;
unsigned long K;
cin >> N >> K;
vector<unsigned> P(N);
for (auto &p : P) {
cin >> p;
--p;
}
// 求める順列
vector<unsigned> ans(N);
// used[i] := i に対する答えをすでに求めていれば true
basic_string used(N, false);
for (unsigned i{}; i < N; ++i) if (!used[i]) {
// i を含むサイクルを求める
vector<unsigned> cycle;
{
unsigned j{i};
while (!used[j]) {
used[j] = true;
cycle.push_back(j);
j = P[j];
}
}
// サイクルに対して答えを求める
const auto cycle_length{size(cycle)};
const auto shift{atcoder::pow_mod(2, K, cycle_length)};
for (unsigned j{}; j < cycle_length; ++j)
ans[cycle[j]] = cycle[(j + shift) % cycle_length];
}
for (const auto p : ans)
cout << p + 1 << " ";
cout << endl;
return 0;
}
#include <atcoder/math> #include <iostream> #include <vector> int main() { using namespace std; unsigned N; unsigned long K; cin >> N >> K; vector<unsigned> P(N); for (auto &p : P) { cin >> p; --p; } // 求める順列 vector<unsigned> ans(N); // used[i] := i に対する答えをすでに求めていれば true basic_string used(N, false); for (unsigned i{}; i < N; ++i) if (!used[i]) { // i を含むサイクルを求める vector<unsigned> cycle; { unsigned j{i}; while (!used[j]) { used[j] = true; cycle.push_back(j); j = P[j]; } } // サイクルに対して答えを求める const auto cycle_length{size(cycle)}; const auto shift{atcoder::pow_mod(2, K, cycle_length)}; for (unsigned j{}; j < cycle_length; ++j) ans[cycle[j]] = cycle[(j + shift) % cycle_length]; } for (const auto p : ans) cout << p + 1 << " "; cout << endl; return 0; }
posted:
last update: