結果

問題 No.2360 Path to Integer
ユーザー 遭難者遭難者
提出日時 2022-11-28 12:11:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 83 ms / 2,500 ms
コード長 1,809 bytes
コンパイル時間 4,345 ms
コンパイル使用メモリ 262,336 KB
実行使用メモリ 18,196 KB
最終ジャッジ日時 2023-09-13 15:48:06
合計ジャッジ時間 6,983 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,124 KB
testcase_01 AC 3 ms
4,972 KB
testcase_02 AC 3 ms
4,908 KB
testcase_03 AC 3 ms
5,028 KB
testcase_04 AC 3 ms
5,004 KB
testcase_05 AC 3 ms
5,004 KB
testcase_06 AC 3 ms
5,096 KB
testcase_07 AC 3 ms
5,140 KB
testcase_08 AC 8 ms
5,396 KB
testcase_09 AC 73 ms
11,396 KB
testcase_10 AC 51 ms
11,748 KB
testcase_11 AC 59 ms
11,748 KB
testcase_12 AC 54 ms
11,472 KB
testcase_13 AC 83 ms
11,480 KB
testcase_14 AC 78 ms
17,164 KB
testcase_15 AC 72 ms
11,212 KB
testcase_16 AC 73 ms
18,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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