結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー tentententen
提出日時 2021-01-20 09:09:25
言語 Java
(openjdk 23)
結果
AC  
実行時間 216 ms / 5,000 ms
コード長 869 bytes
コンパイル時間 2,792 ms
コンパイル使用メモリ 76,860 KB
実行使用メモリ 43,416 KB
最終ジャッジ日時 2024-12-21 16:10:05
合計ジャッジ時間 11,887 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

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