結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー hotpepsi
提出日時 2015-08-30 00:59:31
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 722 bytes
コンパイル時間 402 ms
コンパイル使用メモリ 59,556 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-18 15:37:00
合計ジャッジ時間 1,332 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <sstream>
#include <numeric>

using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return (a / gcd(a, b)) * b; }

int main(int argc, char *argv[])
{
	int N, K;
	int x[1000] = {}, y[1000] = {}, z[1000];
	cin >> N >> K;
	for (int i = 0; i < K; ++i) {
		cin >> x[i] >> y[i];
	}
	for (int i = 0; i < N; ++i) {
		int n = i + 1;
		for (int j = 0; j < K; ++j) {
			if (n == x[j]) {
				++n;
			} else if (n == y[j]) {
				--n;
			}
		}
		z[i] = n - 1;
	}
	int ans = 1;
	for (int i = 0; i < N; ++i) {
		int c;
		int n = z[i];
		for (c = 1; i != n; ++c) {
			n = z[n];
		}
		ans = lcm(ans, c);
	}
	cout << ans << endl;
	return 0;
}
0