結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー GOTKAKO
提出日時 2025-07-03 15:38:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 733 bytes
コンパイル時間 1,901 ms
コンパイル使用メモリ 199,136 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-07-03 15:38:53
合計ジャッジ時間 3,218 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N,K; cin >> N >> K;
    vector<int> P(N);
    iota(P.begin(),P.end(),0);
    while(K--){
        int x,y; cin >> x >> y; x--; y--;
        swap(P.at(x),P.at(y));
    }

    vector<bool> Len(N+1);
    long long answer = 1;
    vector<bool> already(N);
    for(int i=0; i<N; i++){
        if(already.at(i)) continue;
        already.at(i) = true;
        int pos = P.at(i),loop = 1;
        while(pos != i) already.at(pos) = true,pos = P.at(pos),loop++;
        if(Len.at(loop)) continue;
        Len.at(loop) = true;
        answer = answer*loop/(gcd(answer,loop)); 
    }
    cout << answer << endl;
}
0