結果

問題 No.2441 行列累乗
ユーザー 👑 kakel-sankakel-san
提出日時 2023-08-25 21:22:47
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 1,634 bytes
コンパイル時間 1,196 ms
コンパイル使用メモリ 65,856 KB
実行使用メモリ 23,944 KB
最終ジャッジ日時 2023-08-25 21:22:51
合計ジャッジ時間 3,496 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
19,920 KB
testcase_01 AC 65 ms
21,888 KB
testcase_02 AC 66 ms
23,884 KB
testcase_03 AC 66 ms
21,860 KB
testcase_04 AC 66 ms
21,804 KB
testcase_05 AC 66 ms
21,980 KB
testcase_06 AC 66 ms
21,924 KB
testcase_07 AC 66 ms
23,932 KB
testcase_08 AC 66 ms
23,944 KB
testcase_09 AC 65 ms
21,888 KB
testcase_10 AC 66 ms
23,888 KB
testcase_11 AC 67 ms
21,844 KB
testcase_12 AC 66 ms
22,020 KB
testcase_13 AC 66 ms
21,940 KB
testcase_14 AC 65 ms
21,768 KB
testcase_15 AC 66 ms
21,828 KB
testcase_16 AC 66 ms
21,900 KB
testcase_17 AC 66 ms
21,888 KB
testcase_18 AC 68 ms
23,900 KB
testcase_19 AC 66 ms
21,908 KB
権限があれば一括ダウンロードができます

ソースコード

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 long[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var m = NArr(2);
        WriteLine(string.Join("\n", Matrix.Pow(m, 3).Select(mi => string.Join(" ", mi))));
    }
    class Matrix
    {
        static int mod = 1_000_000_007;
        // 行列の累乗
        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