結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー ぴろずぴろず
提出日時 2014-12-11 23:38:59
言語 Java19
(openjdk 21)
結果
AC  
実行時間 198 ms / 5,000 ms
コード長 961 bytes
コンパイル時間 3,064 ms
コンパイル使用メモリ 76,776 KB
実行使用メモリ 60,448 KB
最終ジャッジ日時 2023-09-02 14:07:24
合計ジャッジ時間 10,496 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
55,848 KB
testcase_01 AC 186 ms
60,448 KB
testcase_02 AC 188 ms
58,264 KB
testcase_03 AC 144 ms
55,784 KB
testcase_04 AC 134 ms
55,832 KB
testcase_05 AC 193 ms
58,820 KB
testcase_06 AC 142 ms
55,880 KB
testcase_07 AC 186 ms
58,176 KB
testcase_08 AC 183 ms
56,316 KB
testcase_09 AC 182 ms
56,056 KB
testcase_10 AC 184 ms
56,292 KB
testcase_11 AC 174 ms
56,244 KB
testcase_12 AC 183 ms
58,664 KB
testcase_13 AC 184 ms
58,628 KB
testcase_14 AC 182 ms
56,248 KB
testcase_15 AC 182 ms
56,484 KB
testcase_16 AC 188 ms
58,728 KB
testcase_17 AC 191 ms
58,216 KB
testcase_18 AC 185 ms
56,328 KB
testcase_19 AC 190 ms
58,644 KB
testcase_20 AC 179 ms
56,380 KB
testcase_21 AC 190 ms
58,508 KB
testcase_22 AC 180 ms
56,156 KB
testcase_23 AC 190 ms
58,496 KB
testcase_24 AC 180 ms
56,008 KB
testcase_25 AC 190 ms
57,916 KB
testcase_26 AC 190 ms
58,276 KB
testcase_27 AC 192 ms
58,356 KB
testcase_28 AC 184 ms
56,068 KB
testcase_29 AC 193 ms
58,608 KB
testcase_30 AC 189 ms
58,460 KB
testcase_31 AC 195 ms
59,084 KB
testcase_32 AC 187 ms
58,268 KB
testcase_33 AC 198 ms
58,376 KB
testcase_34 AC 122 ms
55,776 KB
testcase_35 AC 122 ms
55,568 KB
testcase_36 AC 127 ms
55,536 KB
testcase_37 AC 141 ms
55,512 KB
testcase_38 AC 141 ms
55,904 KB
testcase_39 AC 142 ms
55,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no101;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] a = new int[n];
		for(int i=0;i<n;i++) {
			a[i] = i;
		}
		for(int i=0;i<k;i++) {
			int x = sc.nextInt() - 1;
			int y = sc.nextInt() - 1;
			int temp = a[x];
			a[x] = a[y];
			a[y] = temp;
		}
		boolean[] used = new boolean[n];
		long ans = 1;
		for(int i=0;i<n;i++) {
			if (used[i]) {
				continue;
			}
			int length = 0;
			int now = i;
			while(true) {
				if (used[now]) {
					break;
				}
				used[now] = true;
				length++;
				now = a[now];
			}
			ans = lcm(ans, length);
		}
		System.out.println(ans);
	}
	public static long gcd(long a,long b) {
		while(b!=0) {
			long r = a%b;
			a = b;
			b = r;
		}
		return a;
	}
	public static long lcm(long a,long b) {
		return a * b / gcd(a,b);
	}
}
0