結果
問題 | No.101 ぐるぐる!あみだくじ! |
ユーザー |
![]() |
提出日時 | 2015-10-02 00:45:21 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 3 ms / 5,000 ms |
コード長 | 1,036 bytes |
コンパイル時間 | 1,508 ms |
コンパイル使用メモリ | 162,532 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-19 19:02:07 |
合計ジャッジ時間 | 2,789 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
#include "bits/stdc++.h" using namespace std; #define int long long #define REP(i, n) for(int i=0; i<(n); i++) int gcd(int a, int b) { if (!b) return a; return gcd(b, a%b); } int lcm(int a, int b) { return a * b / gcd(a, b); } int N,K; signed main() { cin >> N>>K; vector<int> amida(N); REP(i,N) amida[i] = i; int x, y; REP(i,K) { cin >> x >> y; swap(amida[x-1], amida[y-1]); } int cnt = 0; vector<int> tmp(N); vector<int> loop(N, 0); REP(i,N) tmp[i] = i; while(true) { cnt++; vector<int> tmp2(N); REP(i,N) { tmp2[i] = tmp[amida[i]]; } bool isok = true; REP(i,N) { if (loop[i] == 0 && tmp2[i] == i) { loop[i] = cnt; } if (loop[i] == 0) isok = false; } if (isok) break; tmp = tmp2; } // 最小公倍数を求める int lcmc = 1; REP(i,N) { lcmc = lcm(lcmc, loop[i]); } cout << lcmc << endl; return 0; }