結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー kroton
提出日時 2014-12-12 01:40:31
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 661 bytes
コンパイル時間 441 ms
コンパイル使用メモリ 55,312 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-11 20:55:26
合計ジャッジ時間 1,539 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;
typedef long long ll;

ll gcd(ll a, ll b){
	if(b == 0)return a;
	return gcd(b, a % b);
}

ll lcm(ll a, ll b){
	return a / gcd(a, b) * b;
}

int N, K;
int nxt[111];

bool used[111];
int dfs(int now){
	if(used[now])return 0;
	used[now] = true;
	return 1 + dfs(nxt[now]);
}

ll solve(){
	ll res = 1;
	for(int i=0;i<N;i++)if(!used[i]){
		res = lcm(res, dfs(i));
	}
	return res;
}

int main() {
	cin >> N >> K;
	for(int i=0;i<N;i++)nxt[i] = i;
	for(int i=0;i<K;i++){
		int X, Y;
		cin >> X >> Y;

		--X;
		--Y;

		swap(nxt[X], nxt[Y]);
	}

	cout << solve() << endl;	
	return 0;
}
0