結果

問題 No.2832 Nana's Fickle Adventure
ユーザー 👑 kakel-sankakel-san
提出日時 2024-08-02 23:17:13
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 655 ms / 3,000 ms
コード長 3,835 bytes
コンパイル時間 7,935 ms
コンパイル使用メモリ 167,176 KB
実行使用メモリ 186,376 KB
最終ジャッジ日時 2024-08-02 23:17:46
合計ジャッジ時間 22,889 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
31,744 KB
testcase_01 AC 197 ms
32,632 KB
testcase_02 AC 64 ms
31,616 KB
testcase_03 AC 322 ms
33,408 KB
testcase_04 AC 217 ms
33,408 KB
testcase_05 AC 76 ms
32,256 KB
testcase_06 AC 63 ms
31,744 KB
testcase_07 AC 65 ms
31,576 KB
testcase_08 AC 91 ms
32,384 KB
testcase_09 AC 75 ms
32,384 KB
testcase_10 AC 92 ms
32,384 KB
testcase_11 AC 332 ms
33,408 KB
testcase_12 AC 318 ms
33,656 KB
testcase_13 AC 66 ms
31,608 KB
testcase_14 AC 62 ms
31,360 KB
testcase_15 AC 61 ms
31,196 KB
testcase_16 AC 62 ms
31,744 KB
testcase_17 AC 66 ms
31,832 KB
testcase_18 AC 655 ms
35,840 KB
testcase_19 AC 584 ms
35,584 KB
testcase_20 AC 654 ms
35,840 KB
testcase_21 AC 602 ms
35,448 KB
testcase_22 AC 511 ms
35,032 KB
testcase_23 AC 620 ms
36,096 KB
testcase_24 AC 608 ms
35,328 KB
testcase_25 AC 558 ms
35,712 KB
testcase_26 AC 608 ms
35,840 KB
testcase_27 AC 74 ms
32,256 KB
testcase_28 AC 91 ms
32,384 KB
testcase_29 AC 334 ms
34,176 KB
testcase_30 AC 455 ms
34,688 KB
testcase_31 AC 555 ms
35,456 KB
testcase_32 AC 201 ms
33,280 KB
testcase_33 AC 66 ms
31,616 KB
testcase_34 AC 67 ms
31,736 KB
testcase_35 AC 89 ms
32,384 KB
testcase_36 AC 61 ms
31,488 KB
testcase_37 AC 575 ms
35,456 KB
testcase_38 AC 67 ms
32,128 KB
testcase_39 AC 316 ms
34,176 KB
testcase_40 AC 189 ms
33,536 KB
testcase_41 AC 316 ms
34,048 KB
testcase_42 AC 340 ms
34,432 KB
testcase_43 AC 562 ms
35,456 KB
testcase_44 AC 73 ms
31,872 KB
testcase_45 AC 68 ms
31,744 KB
testcase_46 AC 72 ms
31,872 KB
testcase_47 AC 63 ms
186,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (94 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 int[] NList => ReadLine().Split().Select(int.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 c = NList;
        var (n, m, x) = (c[0], c[1], c[2]);
        var map = NMap(m);
        WriteLine(string.Join("\n", Adv(n, m, x, map)));
    }
    static long[] Adv(int n, int m, int x, int[][] map)
    {
        var move = new int[n][];
        for (var i = 0; i < n; ++i) move[i] = new int[n];
        foreach (var road in map)
        {
            ++move[road[0]][road[1]];
            if (road[0] != road[1]) ++move[road[1]][road[0]];
        }
        var p = new long[n * n][];
        for (var i = 0; i < p.Length; ++i) p[i] = new long[n * n];
        for (var i = 0; i < n; ++i) for (var j = 0; j < n; ++j)
        {
            // yesterday: i, today: j, tomorrow: k
            if (move[i][j] == 0) continue;
            var jm = (int[]) move[j].Clone();
            --jm[i];
            var sum = jm.Sum();
            if (sum == 0)
            {
                p[i * n + j][j * n + j] = 1;
                p[j * n + j][j * n + i] = 1;
            }
            else
            {
                var rev = Exp(sum, mod - 2);
                for (var k = 0; k < n; ++k)
                {
                    p[i * n + j][j * n + k] = jm[k] * rev % mod;
                }
            }
        }
        var a = new long[][] { new long[n * n] };
        {
            var im = (int[]) move[0].Clone();
            var sum = im.Sum();
            if (sum == 0)
            {
                var ans2 = new long[n];
                ans2[0] = 1;
                return ans2;
            }
            var rev = Exp(sum, mod - 2);
            for (var j = 0; j < n; ++j) a[0][j] = im[j] * rev % mod;
        }
        var b = Matrix.Mul(a, Matrix.Pow(p, x - 1));
        var ans = new long[n];
        for (var i = 0; i < n; ++i) for (var j = 0; j < n; ++j) ans[j] = (ans[j] + b[0][i * n + j]) % mod;
        return ans;
    }
    static int mod = 998_244_353;
    static long Exp(long n, long p)
    {
        long _n = n % mod;
        var _p = p;
        var result = 1L;
        if ((_p & 1) == 1) result *= _n;
        while (_p > 0)
        {
            _n = _n * _n % mod;
            _p >>= 1;
            if ((_p & 1) == 1) result = result * _n % mod;
        }
        return result;
    }
    class Matrix
    {
        // 行列の累乗
        public static long[][] Pow(long[][] m, long k)
        {
            var multi = m;
            var r = new long[m.Length][];
            for (var i = 0; i < m.Length; ++i)
            {
                r[i] = new long[m.Length];
                r[i][i] = 1;
            }
            while (k > 0)
            {
                if ((k & 1) == 1) r = Mul(r, multi);
                multi = Mul(multi, multi);
                k >>= 1;
            }
            return r;
        }
        // 行列の積
        public static long[][] Mul(long[][] x, long[][] y)
        {
            var r = new long[x.Length][];
            for (var i = 0; i < x.Length; ++i) r[i] = new long[y[0].Length];
            for (var i = 0; i < x.Length; ++i) for (var k = 0; k < x[0].Length; ++k)
            {
                for (var j = 0; j < y[0].Length; ++j) r[i][j] = (r[i][j] + x[i][k] * y[k][j]) % mod;
            }
            return r;
        }
    }
}
0