結果

問題 No.1704 Many Bus Stops (easy)
ユーザー simansiman
提出日時 2022-01-24 08:22:47
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 876 bytes
コンパイル時間 191 ms
コンパイル使用メモリ 11,544 KB
実行使用メモリ 20,184 KB
最終ジャッジ日時 2023-08-20 17:55:51
合計ジャッジ時間 4,217 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
19,528 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Integer
  def mod_inverse(mod)
    self.pow(mod - 2, mod)
  end
end

T = gets.to_i

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 = [
  [1, 0, 0, 0, 1, 1],
  [0, 1, 0, 1, 0, 1], 
  [0, 0, 1, 1, 1, 0], 
  [3, 0, 0, 0, 0, 0], 
  [0, 3, 0, 0, 0, 0], 
  [0, 0, 3, 0, 0, 0], 
]

T.times do
  n = gets.to_i

  a = pow(A, n)
  m = 3.pow(n + 1, MOD).mod_inverse(MOD)
  b = mul(a, [[1], [0], [0], [3], [0], [0]])
  puts m * b[3][0] % MOD
end
0