結果

問題 No.2131 Concon Substrings (COuNt Version)
ユーザー maguroflymagurofly
提出日時 2022-11-26 19:18:47
言語 Ruby
(3.3.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 1,095 bytes
コンパイル時間 221 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 12,416 KB
最終ジャッジ日時 2024-04-14 05:55:58
合計ジャッジ時間 2,779 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
12,288 KB
testcase_01 AC 87 ms
12,288 KB
testcase_02 AC 86 ms
12,160 KB
testcase_03 AC 86 ms
12,160 KB
testcase_04 AC 87 ms
12,160 KB
testcase_05 AC 86 ms
12,160 KB
testcase_06 AC 87 ms
12,160 KB
testcase_07 AC 87 ms
12,288 KB
testcase_08 AC 87 ms
12,160 KB
testcase_09 AC 87 ms
12,160 KB
testcase_10 AC 88 ms
12,160 KB
testcase_11 AC 86 ms
12,288 KB
testcase_12 AC 87 ms
12,416 KB
testcase_13 AC 87 ms
12,160 KB
testcase_14 AC 87 ms
12,416 KB
testcase_15 AC 87 ms
12,160 KB
testcase_16 AC 87 ms
12,416 KB
testcase_17 AC 88 ms
12,160 KB
testcase_18 AC 87 ms
12,160 KB
testcase_19 AC 88 ms
12,160 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

main = -> {
    N = gets.to_i
    A = ModMatrix.new([
        [25, 0, 0, 0, 0],
        [1, 25, 0, 1, 0],
        [0, 1, 25, 0, 0],
        [0, 0, 1, 25, 0],
        [0, 0, 1, 0, 26],
        ])
    x = ModMatrix.new([[1], [0], [0], [0], [0]])
    B = A**N
    puts (B * x)[4, 0]
}

MOD = 998244353

class ModMatrix
  attr_reader :n, :m, :rows
  def initialize(rows)
    @n, @m, @rows = rows.size, rows[0].size, rows
  end

  def self.id(n)
    new((0 ... n).map { |i|
      (0 ... n).map { |j| (i == j) ? 1 : 0 }
    })
  end

  def [](i, j)
    @rows[i][j]
  end

  def +(other)
    self.class.new(@rows.map.with_index { |row, i|
      row.map.with_index { |x, j| (x + other[i, j]) % MOD }
    })
  end

  def *(other)
    raise "dimension mismatch" unless @m == other.n
    self.class.new((0 ... @n).map { |i|
      (0 ... other.m).map { |j|
        (0 ... @m).sum { |k| self[i, k] * other[k, j] } % MOD
      }
    })
  end

  def **(e)
    r = self.class.id(@n)
    x = self
    while e > 0
      r *= x if (e & 1) == 1
      x *= x if e > 1
      e >>= 1
    end
    r
  end
end

main.call
0