結果
問題 | No.2497 GCD of LCMs |
ユーザー | tenten |
提出日時 | 2023-10-12 08:48:43 |
言語 | Java21 (openjdk 21) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,751 bytes |
コンパイル時間 | 3,472 ms |
コンパイル使用メモリ | 88,240 KB |
実行使用メモリ | 46,716 KB |
最終ジャッジ日時 | 2024-09-14 07:48:10 |
合計ジャッジ時間 | 10,349 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 66 ms
37,912 KB |
testcase_01 | AC | 69 ms
38,000 KB |
testcase_02 | AC | 115 ms
41,492 KB |
testcase_03 | AC | 70 ms
37,940 KB |
testcase_04 | AC | 72 ms
38,140 KB |
testcase_05 | AC | 67 ms
37,672 KB |
testcase_06 | TLE | - |
testcase_07 | TLE | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
ソースコード
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)); 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(); } }