結果

問題 No.2360 Path to Integer
ユーザー pointNpointN
提出日時 2023-06-23 23:38:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 168 ms / 2,500 ms
コード長 2,333 bytes
コンパイル時間 1,508 ms
コンパイル使用メモリ 143,500 KB
実行使用メモリ 30,264 KB
最終ジャッジ日時 2023-09-13 18:48:37
合計ジャッジ時間 3,827 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
14,776 KB
testcase_01 AC 12 ms
14,736 KB
testcase_02 AC 12 ms
14,840 KB
testcase_03 AC 12 ms
14,484 KB
testcase_04 AC 13 ms
14,440 KB
testcase_05 AC 13 ms
14,480 KB
testcase_06 AC 12 ms
14,688 KB
testcase_07 AC 13 ms
14,796 KB
testcase_08 AC 24 ms
15,772 KB
testcase_09 AC 168 ms
24,680 KB
testcase_10 AC 126 ms
25,460 KB
testcase_11 AC 131 ms
25,396 KB
testcase_12 AC 130 ms
24,848 KB
testcase_13 AC 167 ms
24,680 KB
testcase_14 AC 159 ms
29,144 KB
testcase_15 AC 147 ms
24,676 KB
testcase_16 AC 144 ms
30,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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_back
using 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;
};
0