結果
問題 | No.2360 Path to Integer |
ユーザー |
|
提出日時 | 2023-06-23 23:38:58 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 172 ms / 2,500 ms |
コード長 | 2,333 bytes |
コンパイル時間 | 1,598 ms |
コンパイル使用メモリ | 139,172 KB |
最終ジャッジ日時 | 2025-02-15 01:40:56 |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 15 |
ソースコード
#include <iostream>#include <string>#include <vector>#include <algorithm>#include <functional>#include <cmath>#include <iomanip>#include <stack>#include <queue>#include <numeric>#include <map>#include <unordered_map>#include <set>#include <fstream>#include <chrono>#include <random>#include <bitset>//#include <atcoder/all>#define rep(i,n) for(int i=0;i<(n);i++)#define all(x) x.begin(), x.end()#define rall(x) x.rbegin(), x.rend()#define sz(x) ((int)(x).size())#define pb push_backusing ll = long long;using namespace std;template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}const ll mod = 998244353;vector<vector<int>> G;vector<ll> dp1;vector<ll> A;vector<ll> len;vector<ll> child;vector<ll> p10(1500000);ll dfs_c(int n, int p){int ans = 1;rep(i,sz(G[n])){int x = G[n][i];if(x==p) continue;ans += dfs_c(x,n);}child[n] = ans;return ans;}ll dfs(int n, int p){ll ans = 0;rep(i,sz(G[n])){int x = G[n][i];if(x==p) continue;ans += dfs(x,n);ans %= mod;}ans *= p10[len[n]]; ans %= mod;ans += child[n] * A[n] % mod; ans %= mod;dp1[n] = ans;return ans;}int main(){int N; cin >> N;A.resize(N);rep(i,N) cin >> A[i];len.resize(N);rep(i,N){len[i] = (ll)sz(to_string(A[i]));}rep(i,N) A[i] %= mod;p10[0] = 1;rep(i,1500000-1){p10[i+1] = p10[i] * 10 % mod;}G.resize(N);rep(i,N-1){int a,b; cin >> a >> b; a--; b--;G[a].pb(b);G[b].pb(a);}dp1.assign(N,-1);child.assign(N,-1);dfs_c(0,-1);dfs(0,-1);vector<int> d(N,-1);vector<ll> dp2(N,-1);queue<int> q;q.push(0); d[0] = 0; dp2[0] = dp1[0];while(!q.empty()){int n = q.front(); q.pop();rep(i,sz(G[n])){int x=G[n][i];if(d[x]>=0) continue;q.push(x);d[x] = d[n]+1;dp2[x] = dp1[x];dp2[x] += (dp2[n] - (dp1[x]*p10[len[n]]%mod+child[x]*A[n]%mod)%mod + mod) %mod * (p10[len[x]]) % mod;dp2[x] += A[x]*(child[0]-child[x]) % mod;dp2[x] %= mod;}}ll ans = 0;rep(i,N) ans += dp2[i];cout << ans % mod << endl;return 0;};