結果
| 問題 | No.3441 Sort Permutation 2 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-02-06 21:29:37 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 382 ms / 2,000 ms |
| コード長 | 1,625 bytes |
| 記録 | |
| コンパイル時間 | 2,527 ms |
| コンパイル使用メモリ | 227,964 KB |
| 実行使用メモリ | 47,868 KB |
| 最終ジャッジ日時 | 2026-02-06 21:29:55 |
| 合計ジャッジ時間 | 16,216 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 41 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
pair<vector<int>,vector<vector<int>>> FunctionalGraph(const vector<int> &A){
//N頂点N辺有向出次数全て1のグラフでreturn{cycle所属番号(-1は未所属),cycle頂点集合}.
int N = A.size(),nowc = 0;
vector<int> belong(N,-1);
vector<vector<int>> cycle;
vector<bool> already(N),visited(N);
for(int i=0; i<N; i++){
if(already.at(i)) continue;
int pos = i;
bool check = true;
while(!visited.at(pos)) visited.at(pos) = true,pos = A.at(pos);
if(already.at(pos)) check = false;
int memo = pos;
pos = i;
while(!already.at(pos)) already.at(pos) = true,pos = A.at(pos);
if(!check) continue;
pos = memo;
vector<int> now = {pos};
belong.at(pos) = nowc; pos = A.at(pos);
while(pos != memo) now.push_back(pos),belong.at(pos) = nowc,pos = A.at(pos);
cycle.emplace_back(now),nowc++;
}
return {belong,cycle};
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int N; cin >> N;
vector<int> P(N);
for(auto &p : P) cin >> p,p--;
vector<vector<int>> D(N);
for(int i=1; i<N; i++) for(int k=i; k<N; k+=i) D.at(k).push_back(i);
auto [belong,cycle] = FunctionalGraph(P);
vector<int> answer(N);
for(auto &c : cycle){
if(c.size() == 1) continue;
int g = 0;
for(int i=1; i<c.size(); i++) g = gcd(g,c.at(i)-c.at(i-1));
for(auto d : D.at(g)) answer.at(d) += c.size()-1;
}
answer.erase(answer.begin());
for(auto a : answer) cout << a << "\n";
}