結果
問題 | No.2360 Path to Integer |
ユーザー |
![]() |
提出日時 | 2023-06-23 23:04:44 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 311 ms / 2,500 ms |
コード長 | 2,132 bytes |
コンパイル時間 | 231 ms |
コンパイル使用メモリ | 82,444 KB |
実行使用メモリ | 114,588 KB |
最終ジャッジ日時 | 2024-07-01 02:51:36 |
合計ジャッジ時間 | 3,818 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 15 |
ソースコード
import sys# sys.setrecursionlimit(200005)# sys.set_int_max_str_digits(1000005)int1 = lambda x: int(x)-1pDB = lambda *x: print(*x, end="\n", file=sys.stderr)p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)def II(): return int(sys.stdin.readline())def LI(): return list(map(int, sys.stdin.readline().split()))def LLI(rows_number): return [LI() for _ in range(rows_number)]def LI1(): return list(map(int1, sys.stdin.readline().split()))def LLI1(rows_number): return [LI1() for _ in range(rows_number)]def SI(): return sys.stdin.readline().rstrip()dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]inf = (1 << 63)-1# md = 10**9+7md = 998244353def rerooting(root=0):uu = []stack = [root]while stack:u = stack.pop()uu.append(u)vv = []# !!!check the format of the "to"!!!for v in to[u]:if v == par[u]: continuevv.append(v)par[v] = ustack.append(v)to[u] = vvfor u in uu[:0:-1]:p=par[u]cnt[p]+=cnt[u]# bottom upfor u in uu[::-1]:p = par[u]if p == -1: continuedp[p] = merge(dp[p], trans_up(dp[u], u))# top downfor u in uu:p = par[u]if p == -1: continuedp[u] = merge(dp[u], trans_down(purge(dp[p], trans_up(dp[u], u)), u))# v is child# a is parent's value# b is child's valuedef trans_up(valb, v):p = par[v]return (valb*pw[l[p]]+a[p]*cnt[v])%mddef trans_down(vala, v):p = par[v]c = n-cnt[v]return (vala*pw[l[v]]+a[v]*c)%mddef merge(a, b):return (a+b)%mddef purge(a, b):return (a-b)%mdpw = [1]for _ in range(20): pw.append(pw[-1]*10%md)n = II()a = [0]*nl = [0]*nfor i, s in enumerate(SI().split()):l[i] = len(s)a[i] = int(s)to=[[] for _ in range(n)]for _ in range(n-1):u,v=LI1()to[u].append(v)to[v].append(u)cnt = [1]*npar = [-1]*ndp = a[:]rerooting()# print(cnt)# print(dp)ans=0for d in dp:ans+=dans%=mdprint(ans)