結果

問題 No.2360 Path to Integer
ユーザー Jeroen Op de BeekJeroen Op de Beek
提出日時 2023-06-23 22:33:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 87 ms / 2,500 ms
コード長 3,725 bytes
コンパイル時間 2,241 ms
コンパイル使用メモリ 208,288 KB
実行使用メモリ 20,408 KB
最終ジャッジ日時 2023-09-13 17:36:21
合計ジャッジ時間 3,984 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 7 ms
4,380 KB
testcase_09 AC 81 ms
10,916 KB
testcase_10 AC 55 ms
11,196 KB
testcase_11 AC 62 ms
11,268 KB
testcase_12 AC 52 ms
10,904 KB
testcase_13 AC 83 ms
10,856 KB
testcase_14 AC 87 ms
18,892 KB
testcase_15 AC 78 ms
10,904 KB
testcase_16 AC 78 ms
20,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e5+1, oo = 1e9;
const long long MD = 998244353;
template<long long MOD=MD> struct mdint {
    int d=0;
    mdint () {d=0;}
    mdint (long long _d) : d(_d%MOD){
        if(d<0) d+=MOD;
    };
    friend mdint& operator+=(mdint& a, const mdint& o) {
        a.d+=o.d; if(a.d>=MOD) a.d-=MOD;
        return a;
    }
    friend mdint& operator-=(mdint& a, const mdint& o) {
        a.d-=o.d; if(a.d<0) a.d+=MOD;
        return a;
    }
    friend mdint& operator*=(mdint& a, const mdint& o) {
        return a = mdint((ll)a.d*o.d);
    }
    mdint operator*(const mdint& o) const {
        mdint res = *this;
        res*=o;
        return res;
    }
    mdint operator+(const mdint& o) const {
        mdint res = *this;
        res+=o;
        return res;
    }
    mdint operator-(const mdint& o) const {
        mdint res = *this;
        res-=o;
        return res;
    }
    mdint operator^(long long b) const {
        mdint tmp = 1;
        mdint power = *this;
        while(b) {
            if(b&1) {
                tmp = tmp*power;
            }
            power = power*power;
            b/=2;
        }
        return tmp;
    }
    friend mdint operator/=(mdint& a, const mdint& o) {
        a *= (o^(MOD-2));
        return a;
    }
    mdint operator/(const mdint& o) {
        mdint res = *this;
        res/=o;
        return res;
    }
    bool operator==(const mdint& o) { return d==o.d;}
    bool operator!=(const mdint& o) { return d!=o.d;}
    friend istream& operator>>(istream& c, mdint& a) {return c >> a.d;}
    friend ostream& operator<<(ostream& c, const mdint& a) {return c << a.d;}
};
using  mint = mdint<MD>;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n; cin >> n;
    vector<mint> a(n);
    vi len(n);
    for(int i=0;i<n;++i) {
        string s; cin >> s;
        len[i]=s.size();
        a[i]=stoll(s);
    }
    vvi adj(n);
    for(int i=1;i<n;++i) {
        int u,v; cin >> u >> v;
        --u,--v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    vector<mint> up(n);
    vector<mint> pw(max(22,n+1),1);
    vi sz(n,1);
    for(int i=1;i<pw.size();++i) pw[i]=pw[i-1]*10;
    auto dfs = [&](auto&& self, int at, int from) -> void {
        up[at]=1;
        for(int to : adj[at]) if(to!=from) {
            self(self,to,at);
            up[at]+=up[to]*pw[len[to]];
            sz[at]+=sz[to];
        }

    };
    dfs(dfs,0,-1);
    vector<mint> down(n);
    mint ans=0;
    auto dfs2 = [&](auto&& self, int at, int from) -> void {
        if(from!=-1) {
            auto cur1 = up[from]-up[at]*pw[len[at]];
            auto cur2 =  down[from];
            // debug(at);
            // debug(cur1);
            // debug(cur2);
            down[at] =(cur1+cur2)*pw[len[from]];
        } else down[at]=0;
        ans+=a[at]*n;
        ans+=(down[at]*sz[at])*a[at];
        for(int to : adj[at]) if(to!=from) {
            ans+=up[to]*pw[len[to]]*(n-sz[to])*a[at];
            self(self,to,at);
        }

    };
    dfs2(dfs2,0,-1);
    cout << ans << '\n';
    
    
}
0