結果

問題 No.2497 GCD of LCMs
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2023-10-07 10:41:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 310 ms / 2,000 ms
コード長 1,263 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 82,104 KB
実行使用メモリ 77,504 KB
最終ジャッジ日時 2024-07-26 17:45:25
合計ジャッジ時間 3,344 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
53,732 KB
testcase_01 AC 41 ms
58,864 KB
testcase_02 AC 44 ms
59,064 KB
testcase_03 AC 36 ms
53,436 KB
testcase_04 AC 35 ms
54,200 KB
testcase_05 AC 35 ms
54,048 KB
testcase_06 AC 45 ms
62,264 KB
testcase_07 AC 115 ms
76,620 KB
testcase_08 AC 158 ms
76,996 KB
testcase_09 AC 173 ms
76,644 KB
testcase_10 AC 266 ms
77,096 KB
testcase_11 AC 175 ms
77,000 KB
testcase_12 AC 248 ms
77,232 KB
testcase_13 AC 295 ms
77,504 KB
testcase_14 AC 187 ms
76,840 KB
testcase_15 AC 183 ms
77,024 KB
testcase_16 AC 310 ms
77,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n,m = map(int,input().split())
A = list(map(int,input().split()))
mod = 998244353
e = [[] for i in range(n)]
for _ in range(m):
    u,v = [int(x)-1 for x in input().split()]
    e[u].append(v)
    e[v].append(u)


divs = set()
for a in A:
    now = a
    for i in range(2,int(a**0.5)+1):
        if now%i:
            continue

        divs.add(i)
        while now%i == 0:
            now //= i

    if now != 1:
        divs.add(now)


ans = [1]*n

def calc(d,ans):

    vis = [0]*n
    ok = [0]*n
    nexs = set()

    nums = [[0,i] for i in range(n)]
    for i,a in enumerate(A):
        while a%d == 0:
            a //= d
            nums[i][0] += 1
    
    nums.sort()

    nexs.add(0)


    for num,ind in nums:
        ok[ind] = 1

        if ind not in nexs:
            continue

        vis[ind] = 1
        q = [ind]

        while q:
            now = q.pop()
            ans[now] *= pow(d,num,mod)
            ans[now] %= mod

            for nex in e[now]:
                if vis[nex]:
                    continue
                if ok[nex]:
                    vis[nex] = 1
                    q.append(nex)
                else:
                    nexs.add(nex)
    return ans

for d in divs:
    ans = calc(d,ans)

for i in ans:
    print(i)
0