結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー htensaihtensai
提出日時 2020-05-14 17:16:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 195 ms / 5,000 ms
コード長 959 bytes
コンパイル時間 2,110 ms
コンパイル使用メモリ 73,616 KB
実行使用メモリ 59,048 KB
最終ジャッジ日時 2023-10-13 15:09:53
合計ジャッジ時間 11,053 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,896 KB
testcase_01 AC 187 ms
58,296 KB
testcase_02 AC 185 ms
58,296 KB
testcase_03 AC 138 ms
56,160 KB
testcase_04 AC 130 ms
55,880 KB
testcase_05 AC 194 ms
58,832 KB
testcase_06 AC 142 ms
55,692 KB
testcase_07 AC 191 ms
58,336 KB
testcase_08 AC 185 ms
56,832 KB
testcase_09 AC 183 ms
56,112 KB
testcase_10 AC 190 ms
56,892 KB
testcase_11 AC 182 ms
56,116 KB
testcase_12 AC 187 ms
58,792 KB
testcase_13 AC 195 ms
58,252 KB
testcase_14 AC 184 ms
56,060 KB
testcase_15 AC 187 ms
56,336 KB
testcase_16 AC 188 ms
59,048 KB
testcase_17 AC 186 ms
58,048 KB
testcase_18 AC 182 ms
56,408 KB
testcase_19 AC 189 ms
58,200 KB
testcase_20 AC 182 ms
56,164 KB
testcase_21 AC 187 ms
57,620 KB
testcase_22 AC 184 ms
56,000 KB
testcase_23 AC 193 ms
58,652 KB
testcase_24 AC 182 ms
56,096 KB
testcase_25 AC 190 ms
58,304 KB
testcase_26 AC 192 ms
58,356 KB
testcase_27 AC 187 ms
57,296 KB
testcase_28 AC 181 ms
56,208 KB
testcase_29 AC 192 ms
58,568 KB
testcase_30 AC 187 ms
58,096 KB
testcase_31 AC 175 ms
58,428 KB
testcase_32 AC 186 ms
58,668 KB
testcase_33 AC 194 ms
58,256 KB
testcase_34 AC 123 ms
55,708 KB
testcase_35 AC 122 ms
55,980 KB
testcase_36 AC 122 ms
55,712 KB
testcase_37 AC 138 ms
57,836 KB
testcase_38 AC 137 ms
55,784 KB
testcase_39 AC 137 ms
56,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] arr = new int[n];
		for (int i = 0; i < n; i++) {
		    arr[i] = i;
		}
		for (int i = 0; i < k; i++) {
		    int x = sc.nextInt() - 1;
		    int y = sc.nextInt() - 1;
		    int tmp = arr[x];
		    arr[x] = arr[y];
		    arr[y] = tmp;
		}
		int[] idxes = new int[n];
		for (int i = 0; i < n; i++) {
		    idxes[arr[i]] = i;
		}
		long ans = 1;
		for (int i = 0; i < n; i++) {
		    int count = 1;
		    int x = i;
		    while (i != idxes[x]) {
		        x = idxes[x];
		        count++;
		    }
		    ans = getLCM(ans, count);
		}
		System.out.println(ans);
	}
	
	static long getLCM(long x, long y) {
	    return x / getGCD(x, y) * y;
	}
	
	static long getGCD(long x, long y) {
	    if (x % y == 0) {
	        return y;
	    } else {
	        return getGCD(y, x % y);
	    }
	}
}
0