結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー takeya_okinotakeya_okino
提出日時 2019-09-07 16:22:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 194 ms / 5,000 ms
コード長 980 bytes
コンパイル時間 2,870 ms
コンパイル使用メモリ 74,256 KB
実行使用メモリ 59,284 KB
最終ジャッジ日時 2023-09-09 04:17:38
合計ジャッジ時間 11,547 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
55,872 KB
testcase_01 AC 187 ms
58,312 KB
testcase_02 AC 184 ms
58,280 KB
testcase_03 AC 136 ms
56,520 KB
testcase_04 AC 130 ms
55,576 KB
testcase_05 AC 188 ms
58,792 KB
testcase_06 AC 136 ms
55,964 KB
testcase_07 AC 180 ms
58,200 KB
testcase_08 AC 184 ms
56,324 KB
testcase_09 AC 166 ms
56,208 KB
testcase_10 AC 179 ms
56,196 KB
testcase_11 AC 176 ms
55,760 KB
testcase_12 AC 185 ms
58,260 KB
testcase_13 AC 194 ms
58,788 KB
testcase_14 AC 183 ms
55,964 KB
testcase_15 AC 179 ms
56,304 KB
testcase_16 AC 182 ms
58,532 KB
testcase_17 AC 183 ms
58,388 KB
testcase_18 AC 182 ms
56,200 KB
testcase_19 AC 182 ms
59,284 KB
testcase_20 AC 180 ms
56,536 KB
testcase_21 AC 185 ms
58,420 KB
testcase_22 AC 184 ms
55,736 KB
testcase_23 AC 188 ms
58,932 KB
testcase_24 AC 176 ms
56,712 KB
testcase_25 AC 187 ms
58,412 KB
testcase_26 AC 189 ms
58,096 KB
testcase_27 AC 188 ms
58,640 KB
testcase_28 AC 177 ms
57,780 KB
testcase_29 AC 189 ms
58,352 KB
testcase_30 AC 185 ms
58,060 KB
testcase_31 AC 189 ms
58,272 KB
testcase_32 AC 180 ms
58,668 KB
testcase_33 AC 185 ms
58,120 KB
testcase_34 AC 119 ms
55,400 KB
testcase_35 AC 122 ms
55,700 KB
testcase_36 AC 124 ms
55,892 KB
testcase_37 AC 131 ms
55,820 KB
testcase_38 AC 134 ms
55,600 KB
testcase_39 AC 130 ms
56,224 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[] sig = new int[n];
    for(int i = 0; i < n; i++) {
      sig[i] = i;
    }
    for(int i = 0; i < k; i++) {
      int x = sc.nextInt() - 1;
      int y = sc.nextInt() - 1;
      int s = sig[x];
      sig[x] = sig[y];
      sig[y] = s;
    }
    int[] sig2 = new int[n];
    for(int i = 0; i < n; i++) {
      sig2[sig[i]] = i;
    }
    int[] b = new int[n];
    for(int i = 0; i < n; i++) {
      int t = 1;
      int p = i;
      while(sig2[p] != i) {
        t++;
        p = sig2[p];
      }
      b[i] = t;
    }
    long lcm = 1;
    for(int i = 0; i < n; i++) {
      long g = gcd(lcm, (long)b[i]);
      lcm = g * (lcm / g) * ((long)b[i] / g);
    }
    System.out.println(lcm);
  }

  public static long gcd(long a, long b) {
    if(b == 0) return a;
    return gcd(b, (a % b));
  }
}
0