結果

問題 No.2360 Path to Integer
ユーザー kakel-san
提出日時 2023-11-09 00:23:43
言語 C#
(.NET 8.0.404)
結果
AC  
実行時間 288 ms / 2,500 ms
コード長 2,845 bytes
コンパイル時間 6,786 ms
コンパイル使用メモリ 167,752 KB
実行使用メモリ 244,560 KB
最終ジャッジ日時 2024-09-25 23:58:01
合計ジャッジ時間 10,932 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 15
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (88 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static long[] NList => ReadLine().Split().Select(long.Parse).ToArray();
    static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray();
    static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var n = NN;
        var a = NList;
        var map = NMap(n - 1);
        var tree = new List<Edge>[n];
        for (var i = 0; i < tree.Length; ++i) tree[i] = new List<Edge>();
        foreach (var edge in map)
        {
            tree[edge[0]].Add(new Edge(edge[1]));
            tree[edge[1]].Add(new Edge(edge[0]));
        }
        var mod = 998_244_353;
        var mul = new long[n];
        for (var i = 0; i < n; ++i)
        {
            mul[i] = 10;
            while (a[i] >= mul[i]) mul[i] *= 10;
            a[i] %= mod;
            mul[i] %= mod;
        }
        var dp = new long[n];
        var count = new long[n];
        DFS1(-1, 0, tree, a, mul, count, dp);
        // WriteLine(string.Join(" ", dp));
        DFS2(-1, 0, 0L, 0L, tree, a, mul, count, dp);
        // WriteLine(string.Join(" ", dp));
        WriteLine(dp.Sum() % mod);
    }
    static int mod = 998_244_353;
    static void DFS1(int prev, int cur, List<Edge>[] tree, long[] a, long[] mul, long[] count, long[] dp)
    {
        var sum = 0L;
        foreach (var next in tree[cur])
        {
            if (prev == next.To) continue;
            DFS1(cur, next.To, tree, a, mul, count, dp);
            count[cur] += count[next.To];
            next.Coi = count[next.To];
            next.Dpi = (dp[next.To] * mul[cur] % mod + a[cur] * count[next.To] % mod) % mod;
            sum = (sum + next.Dpi) % mod;
        }
        ++count[cur];
        dp[cur] = (sum + a[cur]) % mod;
    }
    static void DFS2(int prev, int cur, long ci, long di, List<Edge>[] tree, long[] a, long[] mul, long[] count, long[] dp)
    {
        var dsum = (di * mul[cur] % mod + a[cur] * ci % mod + dp[cur]) % mod;
        var csum = (ci + count[cur]) % mod;
        foreach (var next in tree[cur])
        {
            if (prev == next.To) continue;
            // WriteLine($"call DFS2 {cur} to {next.To}, ci = {csum - next.Coi}, di = {dsum - next.Dpi}");
            DFS2(cur, next.To, (csum - next.Coi + mod) % mod, (dsum - next.Dpi + mod) % mod, tree, a, mul, count, dp);
        }
        dp[cur] = dsum;
    }
    class Edge
    {
        public int To;
        public long Dpi;
        public long Coi;
        public Edge(int to)
        {
            To = to;
        }
    }
}
0