結果
問題 | No.2360 Path to Integer |
ユーザー |
![]() |
提出日時 | 2022-11-28 12:11:34 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 56 ms / 2,500 ms |
コード長 | 1,809 bytes |
コンパイル時間 | 3,393 ms |
コンパイル使用メモリ | 251,928 KB |
最終ジャッジ日時 | 2025-02-09 01:49:20 |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 15 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; #include <atcoder/all> using namespace atcoder; #define mint modint998244353 #define rep(i, n) for (int i = 0; i < n; i++) #define endl '\n' constexpr int inf = 100000; mint ten[11]; int al[inf]; ll a[inf]; mint xx1[inf], yy1[inf], xx2[inf], yy2[inf]; mint ans = 0; void f1(int to, int no, vector<vector<int>> &g) { xx1[to] = yy1[to] = 1; for (int i : g[to]) { if (i == no) continue; f1(i, to, g); xx1[to] += xx1[i]; yy1[to] += yy1[i]; } yy1[to] *= ten[al[to]]; } void f2(int to, int no, vector<vector<int>> &g) { mint xx = 1, yy = 1; for (int i : g[to]) { if (i == no) { xx += xx2[to]; yy += yy2[to]; continue; } xx += xx1[i]; yy += yy1[i]; } mint aans = 0; for (int i : g[to]) { if (i == no) { aans += yy2[to] * xx1[to]; continue; } xx2[i] = xx - xx1[i]; yy2[i] = (yy - yy1[i]) * ten[al[to]]; aans += yy1[i] * xx2[i]; f2(i, to, g); } ans += aans * a[to]; } int ff(ll n) { return n == 0 ? 0 : ff(n / 10) + 1; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); ten[0] = 1; for (int i = 1; i <= 10; i++) ten[i] = 10 * ten[i - 1]; int n; cin >> n; rep(i, n) { cin >> a[i]; al[i] = ff(a[i]); ans += n * a[i]; } vector<vector<int>> g(n); rep(i, n - 1) { int u, v; cin >> u >> v; u--, v--; g[u].push_back(v); g[v].push_back(u); } f1(0, -1, g); f2(0, -1, g); cout << ans.val() << endl; return 0; }