結果
問題 | No.101 ぐるぐる!あみだくじ! |
ユーザー |
![]() |
提出日時 | 2019-09-07 16:22:48 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 195 ms / 5,000 ms |
コード長 | 980 bytes |
コンパイル時間 | 2,125 ms |
コンパイル使用メモリ | 76,976 KB |
実行使用メモリ | 43,676 KB |
最終ジャッジ日時 | 2024-06-26 21:25:00 |
合計ジャッジ時間 | 10,199 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
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)); } }