結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー kano_townkano_town
提出日時 2014-12-12 00:08:57
言語 Java21
(openjdk 21)
結果
AC  
実行時間 217 ms / 5,000 ms
コード長 1,058 bytes
コンパイル時間 3,552 ms
コンパイル使用メモリ 75,680 KB
実行使用メモリ 59,432 KB
最終ジャッジ日時 2023-09-02 14:13:25
合計ジャッジ時間 12,643 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,756 KB
testcase_01 AC 189 ms
58,536 KB
testcase_02 AC 187 ms
58,544 KB
testcase_03 AC 160 ms
55,848 KB
testcase_04 AC 132 ms
55,744 KB
testcase_05 AC 202 ms
58,924 KB
testcase_06 AC 143 ms
56,276 KB
testcase_07 AC 191 ms
58,632 KB
testcase_08 AC 190 ms
56,404 KB
testcase_09 AC 201 ms
56,644 KB
testcase_10 AC 200 ms
57,000 KB
testcase_11 AC 198 ms
56,588 KB
testcase_12 AC 209 ms
58,732 KB
testcase_13 AC 202 ms
58,672 KB
testcase_14 AC 199 ms
56,272 KB
testcase_15 AC 209 ms
58,936 KB
testcase_16 AC 209 ms
58,704 KB
testcase_17 AC 202 ms
58,308 KB
testcase_18 AC 197 ms
57,092 KB
testcase_19 AC 207 ms
58,908 KB
testcase_20 AC 199 ms
56,260 KB
testcase_21 AC 204 ms
58,668 KB
testcase_22 AC 204 ms
56,496 KB
testcase_23 AC 217 ms
59,076 KB
testcase_24 AC 205 ms
56,404 KB
testcase_25 AC 206 ms
58,780 KB
testcase_26 AC 215 ms
59,432 KB
testcase_27 AC 216 ms
58,932 KB
testcase_28 AC 202 ms
56,592 KB
testcase_29 AC 216 ms
59,008 KB
testcase_30 AC 211 ms
59,056 KB
testcase_31 AC 205 ms
58,640 KB
testcase_32 AC 207 ms
58,692 KB
testcase_33 AC 214 ms
58,472 KB
testcase_34 AC 126 ms
55,952 KB
testcase_35 AC 125 ms
55,780 KB
testcase_36 AC 127 ms
55,744 KB
testcase_37 AC 152 ms
55,708 KB
testcase_38 AC 150 ms
56,072 KB
testcase_39 AC 146 ms
55,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Yukicoder101 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[][] xy = new int[k + 1][2];
		for (int i = 0; i < k; i++) {
			xy[i + 1][0] = sc.nextInt();
			xy[i + 1][1] = sc.nextInt();
		}

		int[] loop = new int[n + 1];
		Arrays.fill(loop, 1);

		int pos, res = 1;
		for (int l = 1; l <= n; l++) {
			pos = l;
			for (int i = 1; i <= n; i++) {
				for (int j = 1; j <= k; j++) {
					if (xy[j][0] == pos) {
						pos = xy[j][1];
					} else if (xy[j][1] == pos) {
						pos = xy[j][0];
					}
				}
				if (l == pos) {
					loop[l] = i;
					break;
				}
			}
		}
		res = loop[1];
		for (int i = 2; i <= n; i++) {
			res = lcm(res, loop[i]);
		}
		System.out.println(res);

	}

	static int gcd(int n, int m) {
		int r = n % m;
		if (r == 0) return m;
		else return gcd(m, r);

	}

	static int lcm(int n, int m) {
		return n / gcd(n, m) * m;
	}

}
0