結果

問題 No.1810 RGB Biscuits
ユーザー simansiman
提出日時 2022-01-20 05:32:29
言語 Ruby
(3.3.0)
結果
AC  
実行時間 862 ms / 2,000 ms
コード長 781 bytes
コンパイル時間 233 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 12,416 KB
最終ジャッジ日時 2024-05-02 20:11:09
合計ジャッジ時間 8,638 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
12,160 KB
testcase_01 AC 95 ms
12,160 KB
testcase_02 AC 90 ms
12,160 KB
testcase_03 AC 102 ms
12,288 KB
testcase_04 AC 104 ms
12,288 KB
testcase_05 AC 795 ms
12,288 KB
testcase_06 AC 849 ms
12,288 KB
testcase_07 AC 862 ms
12,288 KB
testcase_08 AC 853 ms
12,288 KB
testcase_09 AC 845 ms
12,288 KB
testcase_10 AC 563 ms
12,416 KB
testcase_11 AC 100 ms
12,160 KB
testcase_12 AC 123 ms
12,160 KB
testcase_13 AC 124 ms
12,288 KB
testcase_14 AC 223 ms
12,416 KB
testcase_15 AC 116 ms
12,288 KB
testcase_16 AC 141 ms
12,416 KB
testcase_17 AC 436 ms
12,288 KB
testcase_18 AC 448 ms
12,288 KB
testcase_19 AC 105 ms
12,160 KB
testcase_20 AC 97 ms
12,288 KB
testcase_21 AC 186 ms
12,288 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

MOD = 10 ** 9 + 7

def mul(a, b)
  ret = Array.new(a.size) { Array.new(b[0].size, 0) }

  a.size.times do |i|
    b.size.times do |k|
      b[0].size.times do |j|
        ret[i][j] = (ret[i][j] + a[i][k] * b[k][j]) % MOD
      end
    end
  end

  ret
end

def pow(a, n)
  h = a.size
  w = a[0].size
  ret = Array.new(h) { Array.new(w, 0) }

  h.times do |i|
    ret[i][i] = 1
  end

  while n > 0
    ret = mul(ret, a) if n % 2 == 1
    a = mul(a, a)
    n /= 2
  end

  ret
end

A, B = gets.split.map(&:to_i)
a = [[1, 0, 0], [0, 1, 0], [A, B, 0]]
b = [[0, 0, 1], [1, 0, 0], [0, 0, 0]]
c = mul(b, a)
N = gets.to_i

N.times do
  t = gets.to_i
  ret = pow(c, t / 2)

  if t.odd?
    ret = mul(a, ret)
  end

  if t == 0
    puts 2
  else
    puts ret.map(&:sum).sum % MOD
  end
end
0