結果

問題 No.3006 ベイカーの問題
ユーザー magurofly
提出日時 2025-01-17 22:46:21
言語 Ruby
(3.4.1)
結果
AC  
実行時間 113 ms / 2,000 ms
コード長 1,162 bytes
コンパイル時間 276 ms
コンパイル使用メモリ 7,808 KB
実行使用メモリ 12,928 KB
最終ジャッジ日時 2025-01-17 22:46:26
合計ジャッジ時間 5,294 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:37: warning: mismatched indentations at 'end' with 'def' at 33
Syntax OK

ソースコード

diff #

main = -> {
    a, b, n = gets.split.map(&:to_i)

    a %= MOD
    b %= MOD

    A = ModMatrix.new([
        [a, -5 * b, 0, 0],
        [b, a, 0, 0],
        [a, -5 * b, 1, 0],
        [b, a, 0, 1],
    ])
    x = ModMatrix.new([
        [a],
        [b],
        [a],
        [b],
    ])

    y = A**(n - 1) * x

    puts "#{y[2, 0]} #{y[3, 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: #{@m} != #{other.n}" 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
        e >>= 1
    end
    r
end
end

main.call
0