結果

問題 No.3250 最小公倍数
コンテスト
ユーザー titia
提出日時 2026-06-21 06:03:20
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
TLE  
実行時間 -
コード長 2,404 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 398 ms
コンパイル使用メモリ 85,120 KB
実行使用メモリ 390,080 KB
最終ジャッジ日時 2026-06-21 06:03:46
合計ジャッジ時間 25,139 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 20 TLE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
input = sys.stdin.readline

# エラトステネスの篩を用いた素因数分解・約数列挙
MAX=10**6+10 # 使いたい最大値を指定

# Sieve[i]で、iの最も小さい1でない約数を返す。
Sieve=[i for i in range(MAX)]

for i in range(2,MAX):
    if Sieve[i]!=i:
        continue
    
    for j in range(i,MAX,i):
        if Sieve[j]==j:
            Sieve[j]=i

# 素因数分解
def fact(x):
    D=dict()
    while x!=1:
        k=Sieve[x]
        if k in D:
            D[k]+=1
        else:
            D[k]=1
        x//=k
    return D

# 約数列挙
def faclist(x):
    LIST=[1]
    while x!=1:
        k=Sieve[x]
        count=0
        while x%k==0:
            count+=1
            x//=k

        LIST2=[]
        for l in LIST:
            for i in range(1,count+1):
                LIST2.append(l*k**i)
        LIST+=LIST2

    return LIST

mod=998244353

n=int(input())
A=list(map(int,input().split()))

E=[[] for i in range(n)]

for i in range(n-1):
    x,y=map(int,input().split())
    x-=1
    y-=1
    E[x].append(y)
    E[y].append(x)

# 木のHL分解+LCA

ROOT=0

QUE=[ROOT] 
Parent=[-1]*n
Parent[ROOT]=n # ROOTの親を定めておく.
Child=[[] for i in range(n)]
TOP_SORT=[] # トポロジカルソート

while QUE: # トポロジカルソートと同時に親を見つける
    x=QUE.pop()
    TOP_SORT.append(x)
    for to in E[x]:
        if Parent[to]==-1:
            Parent[to]=x
            Child[x].append(to)
            QUE.append(to)

DP=[(dict(),1) for i in range(n)]

ANS=[-1]*n

for x in TOP_SORT[::-1]:
    score=A[x]
    F=fact(score)
    
    if len(Child[x])==0:
        ANS[x]=score
        DP[x]=(score,F)
    else:
        for i in range(len(Child[x])):
            k=Child[x][i]

            score2,F2=DP[k]

            if len(F)<len(F2):
                F,F2=F2,F
                score=score2

            for f in F2:
                if f in F:
                    if F[f]>=F2[f]:
                        continue
                    else:
                        plus=F2[f]-F[f]
                        score=score*pow(f,plus)%mod
                        F[f]=F2[f]
                else:
                    F[f]=F2[f]
                    score=score*pow(f,F[f])%mod
                    
                    


        ANS[x]=score
        DP[x]=(score,F)

            

print("\n".join(map(str,ANS)))
        
        

0