結果

問題 No.2497 GCD of LCMs
ユーザー ks2mks2m
提出日時 2023-10-06 23:02:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 374 ms / 2,000 ms
コード長 1,668 bytes
コンパイル時間 2,470 ms
コンパイル使用メモリ 75,400 KB
実行使用メモリ 58,188 KB
最終ジャッジ日時 2023-10-06 23:02:57
合計ジャッジ時間 6,267 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,696 KB
testcase_01 AC 45 ms
49,596 KB
testcase_02 AC 48 ms
49,684 KB
testcase_03 AC 45 ms
49,804 KB
testcase_04 AC 45 ms
49,812 KB
testcase_05 AC 44 ms
49,648 KB
testcase_06 AC 52 ms
49,776 KB
testcase_07 AC 136 ms
55,868 KB
testcase_08 AC 243 ms
57,564 KB
testcase_09 AC 285 ms
57,496 KB
testcase_10 AC 285 ms
58,128 KB
testcase_11 AC 209 ms
57,056 KB
testcase_12 AC 322 ms
57,892 KB
testcase_13 AC 374 ms
58,188 KB
testcase_14 AC 146 ms
56,124 KB
testcase_15 AC 113 ms
53,312 KB
testcase_16 AC 345 ms
56,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int m = Integer.parseInt(sa[1]);
		sa = br.readLine().split(" ");
		BigInteger[] a = new BigInteger[n];
		for (int i = 0; i < n; i++) {
			a[i] = new BigInteger(sa[i]);
		}
		List<List<Integer>> list = new ArrayList<>(n);
		for (int i = 0; i < n; i++) {
			list.add(new ArrayList<>());
		}
		for (int i = 0; i < m; i++) {
			sa = br.readLine().split(" ");
			int u = Integer.parseInt(sa[0]) - 1;
			int v = Integer.parseInt(sa[1]) - 1;
			list.get(u).add(v);
			list.get(v).add(u);
		}
		br.close();

		BigInteger[] dp = new BigInteger[n];
		boolean[] visit = new boolean[n];
		Queue<Integer> que = new ArrayDeque<>();
		que.add(0);
		dp[0] = a[0];
		visit[0] = true;
		while (!que.isEmpty()) {
			int cur = que.poll();
			for (int next : list.get(cur)) {
				BigInteger g1 = dp[cur].gcd(a[next]);
				BigInteger v1 = dp[cur].multiply(a[next]).divide(g1);
				if (visit[next]) {
					BigInteger g2 = v1.gcd(dp[next]);
					if (!g2.equals(dp[next])) {
						dp[next] = g2;
						que.add(next);
					}
				} else {
					visit[next] = true;
					dp[next] = v1;
					que.add(next);
				}
			}
		}

		BigInteger mod = BigInteger.valueOf(998244353);
		for (int i = 0; i < n; i++) {
			System.out.println(dp[i].mod(mod).intValue());
		}
	}
}
0