結果
問題 | No.101 ぐるぐる!あみだくじ! |
ユーザー |
![]() |
提出日時 | 2015-11-04 15:26:23 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 3 ms / 5,000 ms |
コード長 | 1,483 bytes |
コンパイル時間 | 744 ms |
コンパイル使用メモリ | 66,392 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-13 12:24:20 |
合計ジャッジ時間 | 1,685 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
#include <iostream>#include <vector>#include <cstdio>#include <string>#include <queue>#include <algorithm>#include <cstring>using namespace std;typedef long long LL;#define MAX_N 101#define MAX_K 1001int N, K;int X[MAX_K], Y[MAX_K];inline LL gcd(LL a, LL b){if(a == 0 || b == 0)return 0;LL c;while(a != 0){c = a; a = b%a; b = c;}return b;}inline LL lcm(LL m, LL n){if(0 == m || 0 == n)return 0;return m/gcd(m,n) * n;}void ExecAmida(vector<int>& AmidaList){for(int k = 0; k < K; k++){int x = X[k], y = Y[k];x--; y--;swap(AmidaList[x], AmidaList[y]);}}void Input(){cin >> N;cin >> K;for(int i = 0; i < K; i++){cin >> X[i] >> Y[i];}}LL CalcAmida(int pos, vector<int> NextList){int prePos = pos;LL ret = 0;while(true){ret++;pos = NextList[pos];if(pos == prePos)break;}return ret;}LL Solve(){vector<int> AmidaList;for(int i = 1; i <= N; i++){AmidaList.push_back(i);}//MakeAmidaExecExecAmida(AmidaList);vector<int> NextList;for(int i = 1; i <= N; i++){for(int j = 0; j < N; j++){if(AmidaList[j] == i){NextList.push_back(j);break;}}}LL ret = 1;bool used[MAX_N];memset(used, 0, sizeof(used));for(int i = 0; i < N; i++){ret = lcm(ret, CalcAmida(i, NextList));}return ret;}int main(){Input();cout << Solve() << endl;return 0;}