結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー tentententen
提出日時 2021-01-20 09:09:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 198 ms / 5,000 ms
コード長 869 bytes
コンパイル時間 3,181 ms
コンパイル使用メモリ 73,088 KB
実行使用メモリ 60,336 KB
最終ジャッジ日時 2023-08-23 12:55:26
合計ジャッジ時間 12,614 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
56,008 KB
testcase_01 AC 194 ms
58,636 KB
testcase_02 AC 193 ms
58,492 KB
testcase_03 AC 143 ms
55,732 KB
testcase_04 AC 134 ms
55,608 KB
testcase_05 AC 198 ms
58,852 KB
testcase_06 AC 144 ms
55,724 KB
testcase_07 AC 187 ms
58,464 KB
testcase_08 AC 188 ms
55,744 KB
testcase_09 AC 183 ms
56,152 KB
testcase_10 AC 188 ms
56,016 KB
testcase_11 AC 184 ms
55,784 KB
testcase_12 AC 195 ms
58,140 KB
testcase_13 AC 194 ms
60,336 KB
testcase_14 AC 186 ms
56,104 KB
testcase_15 AC 187 ms
56,040 KB
testcase_16 AC 189 ms
57,944 KB
testcase_17 AC 188 ms
58,116 KB
testcase_18 AC 183 ms
56,284 KB
testcase_19 AC 186 ms
58,876 KB
testcase_20 AC 181 ms
56,832 KB
testcase_21 AC 186 ms
58,012 KB
testcase_22 AC 184 ms
55,872 KB
testcase_23 AC 195 ms
58,288 KB
testcase_24 AC 174 ms
55,996 KB
testcase_25 AC 190 ms
58,556 KB
testcase_26 AC 192 ms
58,316 KB
testcase_27 AC 194 ms
58,368 KB
testcase_28 AC 183 ms
53,984 KB
testcase_29 AC 193 ms
58,396 KB
testcase_30 AC 188 ms
58,152 KB
testcase_31 AC 192 ms
58,348 KB
testcase_32 AC 188 ms
57,992 KB
testcase_33 AC 191 ms
58,432 KB
testcase_34 AC 123 ms
56,116 KB
testcase_35 AC 124 ms
55,952 KB
testcase_36 AC 125 ms
55,572 KB
testcase_37 AC 141 ms
55,672 KB
testcase_38 AC 142 ms
56,316 KB
testcase_39 AC 142 ms
55,848 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[] places = new int[n];
		for (int i = 0; i < n; i++) {
		    places[i] = i;
		}
		int k = sc.nextInt();
		for (int i = 0; i < k; i++) {
		    int x = sc.nextInt();
		    sc.nextInt();
		    int tmp = places[x];
		    places[x] = places[x - 1];
		    places[x - 1] = tmp;
		}
		long ans = 1;
		for (int i = 0; i < n; i++) {
		    int count = 1;
		    int idx = i;
		    while ((idx = places[idx]) != i) {
		        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