luogu#P10706. 悲哀(Sorrow)

悲哀(Sorrow)

Background

Youareanangel,You are an angel, descendingamidlightandholyhymns.descending amid light and holy hymns. Iamademon,I am a demon, crawlingoutofbloodandfilthymud.crawling out of blood and filthy mud. Iwanttoholdyou,I want to hold you, butIfearmybloodbut I fear my blood willstainyourpurewhitewingsred.will stain your pure white wings red.

Problem Description

Given a tree with nn nodes rooted at node 11. Each node has two weights ai,bia_i, b_i (1in1 \le i \le n). The values aia_i are given, and all bib_i are initially 00.

Now, for every pair of nodes (u,v)(u, v) (1u<vn1 \le u < v \le n), let x=LCA(u,v)x = \operatorname{LCA}(u, v). If gcd(au,av)>1\gcd(a_u, a_v) > 1, then set bxbx+au×avb_x \gets b_x + a_u \times a_v; otherwise, do nothing.

For each 1in1 \le i \le n, output bimod998244353b_i \bmod 998244353.

Input Format

The first line contains an integer nn, meaning there are nn nodes.

The second line contains nn positive integers, representing a1,a2,,ana_1, a_2, \dots, a_n.

The next n1n - 1 lines each contain two positive integers u,vu, v, indicating there is an edge between node uu and node vv.

Output Format

Output nn lines, each containing one integer.

The ii-th line should be the value of bib_i modulo 998244353998244353.

8
3 7 2 2 2 3 72 24 
1 2
1 3
1 4
4 5
2 6
5 7
4 8

785
0
0
1972
144
0
0
0
15
73 83 31 9514 1189 43 79 2 2 1798 5063 2 5 2573 53 
1 2
2 3
1 4
4 5
1 6
3 7
5 8
5 9
6 10
7 11
10 12
9 13
7 14
13 15

23952214
633788
79763
38056
4
0
13027099
0
0
3596
0
0
0
0
0

Hint

Sample Explanation

The constructed tree is shown in the figure.

The following contributions exist:

  • For node 11:

(3,4)(3, 4) contributes 44.

(3,5)(3, 5) contributes 44.

(3,7)(3, 7) contributes 144144.

(3,8)(3, 8) contributes 4848.

(1,6)(1, 6) contributes 99.

(1,7)(1, 7) contributes 216216.

(1,8)(1, 8) contributes 7272.

(6,7)(6, 7) contributes 216216.

(6,8)(6, 8) contributes 7272.

Total: 785785.

  • For node 44:

(4,5)(4, 5) contributes 44.

(4,7)(4, 7) contributes 144144.

(4,8)(4, 8) contributes 4848.

(5,8)(5, 8) contributes 4848.

(7,8)(7, 8) contributes 17281728.

Total: 19721972.

  • For node 55:

(5,7)(5, 7) contributes 144144.

Total: 144144.

All other nodes are clearly 00.

Constraints

Subtask ID nn aia_i Special Property Score
00 100100 1000\le 1000 - 55
11 20002000 105\le 10^5 1010
22 10510^5 5×105\le 5 \times 10^5 AA 2525
33 2×1052 \times 10^5 BB 3030
44 -

Special property AA: it is guaranteed that all aia_i are generated randomly.

Special property BB: it is guaranteed that the tree is a complete binary tree.

For 100%100\% of the testdata: 1n2×1051 \le n \le 2 \times 10^5, 1ai5×1051 \le a_i \le 5 \times 10^5.

Special note: This problem uses bundled subtask testing. You will only get the score for a subtask if you pass all test points in that subtask.

Translated by ChatGPT 5