結果

問題 No.2360 Path to Integer
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-07-10 00:57:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,924 bytes
コンパイル時間 2,082 ms
コンパイル使用メモリ 203,948 KB
実行使用メモリ 22,840 KB
最終ジャッジ日時 2023-10-01 04:59:37
合計ジャッジ時間 5,471 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
9,952 KB
testcase_01 AC 6 ms
10,132 KB
testcase_02 AC 7 ms
10,180 KB
testcase_03 AC 6 ms
10,168 KB
testcase_04 AC 7 ms
9,992 KB
testcase_05 AC 6 ms
9,896 KB
testcase_06 AC 6 ms
10,100 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 120 ms
14,036 KB
testcase_11 AC 127 ms
14,032 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 149 ms
13,916 KB
testcase_16 AC 139 ms
22,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const ll modc = 998244353;

ll N, ans=0;
vector<ll> xx1(1e5), yy1(1e5), xx2(1e5), yy2(1e5), a(1e5), ten(1e5);
vector<vector<ll>> E(1e5);

void dfs1(ll from, ll p){
    xx1[from] = 1; yy1[from] = 1;
    for (auto to : E[from]){
        if (to == p) continue;
        dfs1(to, from);
        xx1[from] += xx1[to];
        xx1[from] %= modc;
        yy1[from] += yy1[to];
        yy1[from] %= modc;
    }
    yy1[from] *= ten[from];
    yy1[from] %= modc;
}

void dfs2(ll from, ll p){
    ll xx = 1, yy = 1;
    for (auto to : E[from]){
        if (to == p){
            xx += xx2[from];
            yy += yy2[from];
            xx %= modc;
            yy %= modc;
            continue;
        }
        xx += xx1[to];
        yy += yy1[to];
        xx %= modc;
        yy %= modc;
    }

    for (auto to : E[from]){
        if (to == p) continue;
        xx2[to] = (xx+modc-xx1[to]) % modc;
        yy2[to] = ((yy+modc-yy1[to]) % modc * ten[from]) % modc;
        dfs2(to, from);
    }
}

void dfs3(ll from, ll p){
    ll cnt = (yy2[from] * xx1[from]) % modc;

    for (auto to : E[from]){
        if (to == p) continue;
        cnt += (xx2[to] * yy1[to]) % modc;
        cnt %= modc;
        dfs3(to, from);
    }

    ans += (cnt * a[from]) % modc;
    ans %= modc;
}

ll ndig(ll X){
    return (X == 0 ? 0 : ndig(X/10)+1);
}

ll tn(ll n){
    ll res = 1;
    for (int i=0; i<n; i++){
        res *= 10;
        res %= modc;
    }
    return res;
}

int main(){

    ll x, y;
    cin >> N;
    for (int i=0; i<N; i++){
        cin >> a[i];
        ans += (a[i] * N) % modc;
        ans %= modc;
        ten[i] = tn(ndig(a[i]));
    }
    for (int i=0; i<N-1; i++){
        cin >> x >> y; x--; y--;
        E[x].push_back(y);
        E[y].push_back(x);
    }

    dfs1(0, -1);
    dfs2(0, -1);
    dfs3(0, -1);

    cout << ans << endl;

    return 0;
}
0