結果

問題 No.2360 Path to Integer
ユーザー ぷらぷら
提出日時 2023-06-23 21:59:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 98 ms / 2,500 ms
コード長 1,500 bytes
コンパイル時間 2,347 ms
コンパイル使用メモリ 205,092 KB
実行使用メモリ 23,636 KB
最終ジャッジ日時 2023-09-13 17:01:17
合計ジャッジ時間 4,137 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 7 ms
4,376 KB
testcase_09 AC 96 ms
12,572 KB
testcase_10 AC 71 ms
12,960 KB
testcase_11 AC 75 ms
12,856 KB
testcase_12 AC 49 ms
12,604 KB
testcase_13 AC 98 ms
12,508 KB
testcase_14 AC 93 ms
21,424 KB
testcase_15 AC 97 ms
12,524 KB
testcase_16 AC 93 ms
23,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

constexpr int mod = 998244353;

void mpl(int &x,int y) {
    x += y;
    if(x >= mod) x -= mod;
}

int f[1000],dp1[100200],dp2[100200];
int ans;

pair<int,int> dfs(int n,int p,vector<string>&A,vector<vector<int>>&ki) {
    long long now = stoll(A[n])%mod;
    int cnt1 = 0,cnt2 = 0;
    for(int i:ki[n]) {
        if(i == p) {
            continue;
        }
        pair<int,int> a = dfs(i,n,A,ki);
        mpl(ans,1ll*dp1[n]*a.second%mod);
        mpl(ans,1ll*cnt1*dp2[i]%mod);
        mpl(ans,1ll*dp1[i]*cnt2%mod);
        mpl(ans,1ll*dp2[n]*a.first%mod);
        mpl(cnt1,a.first);
        mpl(cnt2,1ll*a.second*f[A[n].size()]%mod);
        mpl(dp1[n],1ll*dp1[i]*f[A[n].size()]%mod);
        mpl(dp1[n],now*a.first%mod);
        mpl(dp2[n],dp2[i]);
        mpl(dp2[n],now*a.second%mod);
    }
    mpl(dp1[n],now);
    mpl(ans,dp1[n]);
    mpl(ans,dp2[n]);
    mpl(dp2[n],now);
    cnt1++;
    mpl(cnt2,f[A[n].size()]);
    return {cnt1,cnt2};
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    f[0] = 1;
    for(int i = 1; i < 100; i++) {
        f[i] = f[i-1]*10ll%mod;
    }
    int N;
    cin >> N;
    vector<string>A(N);
    for(int i = 0; i < N; i++) {
        cin >> A[i];
    }
    vector<vector<int>>ki(N);
    for(int i = 0; i < N-1; i++) {
        int u,v;
        cin >> u >> v;
        u--;
        v--;
        ki[u].push_back(v);
        ki[v].push_back(u);
    }
    dfs(0,-1,A,ki);
    cout << ans << "\n";
}
0