結果

問題 No.2360 Path to Integer
ユーザー 👑 kakel-sankakel-san
提出日時 2023-11-09 00:23:43
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 384 ms / 2,500 ms
コード長 2,845 bytes
コンパイル時間 7,625 ms
コンパイル使用メモリ 156,688 KB
実行使用メモリ 231,844 KB
最終ジャッジ日時 2023-11-09 00:23:57
合計ジャッジ時間 12,712 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
32,932 KB
testcase_01 AC 83 ms
32,932 KB
testcase_02 AC 84 ms
32,932 KB
testcase_03 AC 84 ms
32,932 KB
testcase_04 AC 83 ms
32,932 KB
testcase_05 AC 83 ms
32,932 KB
testcase_06 AC 82 ms
32,932 KB
testcase_07 AC 99 ms
33,956 KB
testcase_08 AC 102 ms
38,948 KB
testcase_09 AC 384 ms
77,028 KB
testcase_10 AC 292 ms
79,412 KB
testcase_11 AC 294 ms
79,340 KB
testcase_12 AC 264 ms
77,152 KB
testcase_13 AC 359 ms
77,024 KB
testcase_14 AC 333 ms
91,748 KB
testcase_15 AC 344 ms
66,468 KB
testcase_16 AC 337 ms
231,844 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (109 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.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