結果

問題 No.1073 無限すごろく
ユーザー simansiman
提出日時 2022-01-25 07:22:33
言語 Ruby
(3.3.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 783 bytes
コンパイル時間 244 ms
コンパイル使用メモリ 11,428 KB
実行使用メモリ 15,476 KB
最終ジャッジ日時 2023-08-21 23:21:01
合計ジャッジ時間 5,394 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,308 KB
testcase_01 AC 81 ms
15,172 KB
testcase_02 AC 82 ms
15,136 KB
testcase_03 AC 80 ms
15,036 KB
testcase_04 AC 80 ms
15,032 KB
testcase_05 AC 80 ms
15,300 KB
testcase_06 AC 80 ms
15,316 KB
testcase_07 AC 83 ms
15,296 KB
testcase_08 AC 80 ms
15,056 KB
testcase_09 AC 82 ms
15,244 KB
testcase_10 AC 79 ms
15,120 KB
testcase_11 AC 81 ms
15,264 KB
testcase_12 AC 80 ms
15,152 KB
testcase_13 AC 82 ms
15,172 KB
testcase_14 AC 82 ms
15,244 KB
testcase_15 AC 82 ms
15,292 KB
testcase_16 AC 83 ms
15,040 KB
testcase_17 AC 83 ms
15,076 KB
testcase_18 AC 84 ms
15,172 KB
testcase_19 AC 82 ms
15,160 KB
testcase_20 AC 82 ms
15,120 KB
testcase_21 AC 81 ms
15,124 KB
testcase_22 AC 82 ms
15,128 KB
testcase_23 AC 84 ms
15,144 KB
testcase_24 AC 84 ms
15,232 KB
testcase_25 AC 84 ms
15,088 KB
testcase_26 AC 83 ms
15,040 KB
testcase_27 AC 83 ms
15,128 KB
testcase_28 AC 85 ms
15,424 KB
testcase_29 AC 84 ms
15,248 KB
testcase_30 AC 84 ms
15,476 KB
testcase_31 AC 84 ms
15,284 KB
testcase_32 AC 87 ms
15,156 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

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

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

N = gets.to_i
B = pow(A, N)

puts 6.pow(N, MOD).mod_inverse(MOD) * B[0][0] % MOD
0