結果
問題 | No.2497 GCD of LCMs |
ユーザー | tenten |
提出日時 | 2023-10-12 09:04:35 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 770 ms / 2,000 ms |
コード長 | 3,814 bytes |
コンパイル時間 | 4,406 ms |
コンパイル使用メモリ | 89,556 KB |
実行使用メモリ | 66,120 KB |
最終ジャッジ日時 | 2024-09-14 08:03:02 |
合計ジャッジ時間 | 9,494 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 68 ms
50,984 KB |
testcase_01 | AC | 71 ms
50,984 KB |
testcase_02 | AC | 73 ms
51,152 KB |
testcase_03 | AC | 71 ms
50,972 KB |
testcase_04 | AC | 70 ms
51,144 KB |
testcase_05 | AC | 71 ms
50,764 KB |
testcase_06 | AC | 77 ms
50,976 KB |
testcase_07 | AC | 266 ms
57,824 KB |
testcase_08 | AC | 420 ms
50,920 KB |
testcase_09 | AC | 478 ms
51,052 KB |
testcase_10 | AC | 468 ms
53,316 KB |
testcase_11 | AC | 288 ms
47,156 KB |
testcase_12 | AC | 646 ms
48,504 KB |
testcase_13 | AC | 670 ms
54,704 KB |
testcase_14 | AC | 235 ms
46,388 KB |
testcase_15 | AC | 218 ms
46,664 KB |
testcase_16 | AC | 770 ms
66,120 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { static ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); static ArrayList<HashMap<Integer, Integer>> counts = new ArrayList<>(); static int[] costs; static final int MOD = 998244353; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int m = sc.nextInt(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); counts.add(new HashMap<>()); } HashSet<Integer> each = new HashSet<>(); for (int i = 0; i < n; i++) { int x = sc.nextInt(); for (int j = 2; j <= Math.sqrt(x); j++) { while (x % j == 0) { counts.get(i).put(j, counts.get(i).getOrDefault(j, 0) + 1); x /= j; each.add(j); } } if (x > 1) { counts.get(i).put(x, counts.get(i).getOrDefault(x, 0) + 1); each.add(x); } } for (int i = 0; i < m; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; graph.get(a).add(b); graph.get(b).add(a); } long[] ans = new long[n]; Arrays.fill(ans, 1); boolean[] used = new boolean[n]; costs = new int[n]; for (int x : each) { Arrays.fill(costs, Integer.MAX_VALUE); check(0, x, 0, used); for (int i = 0; i < n; i++) { ans[i] *= pow(x, costs[i]); ans[i] %= MOD; } } System.out.println(String.join("\n", Arrays.stream(ans).mapToObj(String::valueOf).toArray(String[]::new))); } static void check(int idx, int value, int current, boolean[] used) { if (used[idx]) { return; } used[idx] = true; int save = current; current = Math.max(current, counts.get(idx).getOrDefault(value, 0)); if (current != costs[idx]) { costs[idx] = Math.min(costs[idx], current); for (int x : graph.get(idx)) { check(x, value, current, used); } } current = save; used[idx] = false; } static long pow(long x, int p) { if (p == 0) { return 1; } else if (p % 2 == 0) { return pow(x * x % MOD, p / 2); } else { return pow(x, p - 1) * x % MOD; } } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }