結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー むらためむらため
提出日時 2019-01-27 02:51:54
言語 Nim
(2.0.2)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,224 bytes
コンパイル時間 2,724 ms
コンパイル使用メモリ 69,768 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 03:02:36
合計ジャッジ時間 3,981 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 8) Warning: imported and not used: 'sequtils' [UnusedImport]

ソースコード

diff #

import sequtils,math

proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': break
    result = 10 * result + k.ord - '0'.ord

const MOD = 1000000007

template useMatrix =
  type Matrix = ref object
    w,h:int
    data: seq[int]
  proc `[]`(m:Matrix,x,y:int):int = m.data[x + y * m.w]
  proc `[]=`(m:var Matrix,x,y,val:int) = m.data[x + y * m.w] = val
  proc newMatrix(w,h:int):Matrix =
    new(result)
    result.w = w
    result.h = h
    result.data = newSeq[int](w * h)
  proc identityMatrix(n:int):Matrix =
    result = newMatrix(n,n)
    for i in 0..<n: result[i,i] = 1
  proc newMatrix(arr:seq[seq[int]]):Matrix =
    new(result)
    result.w = arr[0].len
    result.h = arr.len
    result.data = newSeqUninitialized[int](result.w * result.h)
    for x in 0..<result.w:
      for y in 0..<result.h:
        result[x,y] = arr[y][x]

  proc `$`(m:Matrix) : string =
    result = ""
    for y in 0..<m.h:
      result &= "["
      for x in 0..<m.w:
        result &= $m[x,y]
        if x != m.w - 1 : result &= ","
      result &= "]"
      if y != m.h - 1 : result &= "\n"
    result &= ""

  proc `*`(a,b:Matrix): Matrix =
    assert a.w == b.h
    result = newMatrix(a.h,b.w)
    for y in 0..<a.h:
      for x in 0..<b.w:
        var n = 0
        for k in 0..<a.w:
          when declared(MOD):
            n = (n + (a[k,y] * b[x,k]) mod MOD) mod MOD
          else:
            n += a[k,y] * b[x,k]
        result[y,x] = n

  proc `*`(a:Matrix,b:seq[int]):seq[int] =
    assert a.w == b.len
    result = newSeq[int](b.len)
    for x in 0..<a.h:
      var n = 0
      for k in 0..<a.w:
        when declared(MOD):
          n = (n + (a[k,x] * b[k]) mod MOD) mod MOD
        else:
          n += a[k,x] * b[k]
      result[x] = n

  proc `^`(m:Matrix,n:int) : Matrix =
    assert m.w == m.h
    if n <= 0 : return identityMatrix(m.w)
    if n == 1 : return m
    let m2 = m^(n div 2)
    if n mod 2 == 0 : return m2 * m2
    return m2 * m2 * m
useMatrix()

proc calcFib(n:int):int = (newMatrix(@[@[1,1],@[1,0]])^(n-1)*(@[1,0]))[0]
# F(n) = f(n+1) * f(n)
let n = scan()
echo (calcFib(n) * calcFib(n+1)) mod MOD
0