結果

問題 No.840 ほむほむほむら
ユーザー keymoonkeymoon
提出日時 2019-06-12 02:52:17
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,148 ms / 4,000 ms
コード長 5,179 bytes
コンパイル時間 2,560 ms
コンパイル使用メモリ 109,568 KB
実行使用メモリ 22,016 KB
最終ジャッジ日時 2024-10-08 05:52:36
合計ジャッジ時間 9,880 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
18,816 KB
testcase_01 AC 25 ms
18,816 KB
testcase_02 AC 45 ms
18,796 KB
testcase_03 AC 195 ms
18,732 KB
testcase_04 AC 24 ms
18,688 KB
testcase_05 AC 25 ms
18,688 KB
testcase_06 AC 27 ms
18,944 KB
testcase_07 AC 72 ms
18,976 KB
testcase_08 AC 293 ms
19,072 KB
testcase_09 AC 30 ms
18,816 KB
testcase_10 AC 26 ms
18,944 KB
testcase_11 AC 33 ms
18,944 KB
testcase_12 AC 110 ms
18,732 KB
testcase_13 AC 692 ms
20,352 KB
testcase_14 AC 113 ms
18,732 KB
testcase_15 AC 25 ms
18,944 KB
testcase_16 AC 35 ms
19,072 KB
testcase_17 AC 181 ms
19,200 KB
testcase_18 AC 1,148 ms
21,888 KB
testcase_19 AC 1,144 ms
22,016 KB
testcase_20 AC 24 ms
18,816 KB
testcase_21 AC 24 ms
18,688 KB
testcase_22 AC 34 ms
18,536 KB
testcase_23 AC 1,127 ms
21,760 KB
testcase_24 AC 37 ms
18,944 KB
testcase_25 AC 25 ms
18,688 KB
testcase_26 AC 40 ms
18,668 KB
testcase_27 AC 945 ms
21,248 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Numerics;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using static System.Math;
using Debug = System.Diagnostics.Debug;
using LayoutKind = System.Runtime.InteropServices.LayoutKind;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;

static class P
{
    static void Main()
    {
        //matrix[i,j]:vector[i]の要素が1であればi行目のj要素に乗算が行われ、そこに遷移する
        var nk = Console.ReadLine().Split().Select(int.Parse).ToArray();
        int n = nk[0];
        int k = nk[1];
        ModMatrix matrix = new ModMatrix(k * k * k, k * k * k);
        //k=2の時
        //  a : 01010101
        // ab : 00110011
        //abc : 00001111
        for (int i = 0; i < matrix.Height; i++)
        {
            int val = i;
            int aCount = val % k;
            val /= k;
            int abCount = val % k;
            val /= k;
            int abcCount = val % k;

            int newACount, newABCount, newABCCount;
            //aが追加された時
            newACount = (aCount + 1) % k;
            newABCount = abCount;
            newABCCount = abcCount;
            matrix[i, (newABCCount * k + newABCount) * k + newACount] += 1;

            //bが追加された時
            newACount = aCount;
            newABCount = (abCount + aCount) % k;
            newABCCount = abcCount;
            matrix[i, (newABCCount * k + newABCount) * k + newACount] += 1;

            //cが追加された時
            newACount = aCount;
            newABCount = abCount;
            newABCCount = (abcCount + abCount) % k;
            matrix[i, (newABCCount * k + newABCount) * k + newACount] += 1;
        }

        ModMatrix res = new ModMatrix(1, k * k * k);
        res[0, 0] = 1;

        long pow = n;
        while (n > 0)
        {
            if ((n & 1) == 1) res *= matrix;
            matrix *= matrix;
            n >>= 1;
        }

        long resSum = 0;
        for (int i = 0; i < k * k; i++) resSum += res[0, i];
        Console.WriteLine(resSum % 998244353);
    }
}

class ModMatrix : IEquatable<ModMatrix>
{
    const int MOD = 998244353;
    public readonly int Height;
    public readonly int Width;
    long[,] data;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public ModMatrix(int height, int width)
    {
        data = new long[height, width];
        Height = height;
        Width = width;
    }
    public long this[int i, int j]
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        get { return data[i, j]; }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        set { if ((data[i, j] = value % MOD) < 0) data[i, j] += MOD; }
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public bool Equals(ModMatrix other)
    {
        if (this.Width != other.Width || this.Height != other.Height) return false;
        for (int i = 0; i < Height; i++) for (int j = 0; j < Width; j++) if (this[i, j] != other[i, j]) return false;
        return true;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModMatrix Add(ModMatrix a, ModMatrix b)
    {
        Debug.Assert(a.Width == b.Width && a.Height == b.Height);
        var res = new ModMatrix(a.Height, a.Width);
        for (int i = 0; i < a.Height; i++) for (int j = 0; j < a.Width; j++) if (MOD <= (res.data[i, j] = a[i, j] + b[i, j])) res.data[i, j] -= MOD;
        return res;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModMatrix Sub(ModMatrix a, ModMatrix b)
    {
        Debug.Assert(a.Width == b.Width && a.Height == b.Height);
        var res = new ModMatrix(a.Height, a.Width);
        for (int i = 0; i < a.Height; i++) for (int j = 0; j < a.Width; j++) if ((res.data[i, j] = a[i, j] - b[i, j]) < 0) res.data[i, j] += MOD;
        return res;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModMatrix Mul(ModMatrix a, ModMatrix b)
    {
        Debug.Assert(a.Width == b.Height);
        var res = new ModMatrix(a.Height, b.Width);
        for (int i = 0; i < a.Height; i++) for (int j = 0; j < b.Width; j++) { for (int k = 0; k < a.Width; k++) res.data[i, j] += a[i, k] * b[k, j] % MOD; res.data[i, j] %= MOD; }
        return res;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModMatrix operator +(ModMatrix a, ModMatrix b) => Add(a, b);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModMatrix operator -(ModMatrix a, ModMatrix b) => Sub(a, b);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModMatrix operator *(ModMatrix a, ModMatrix b) => Mul(a, b);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool operator ==(ModMatrix a, ModMatrix b) => a.Equals(b);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool operator !=(ModMatrix a, ModMatrix b) => !a.Equals(b);
}
0