結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー Kutimoti_TKutimoti_T
提出日時 2018-10-19 21:26:28
言語 Nim
(2.0.2)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,345 bytes
コンパイル時間 2,973 ms
コンパイル使用メモリ 70,984 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 20:25:43
合計ジャッジ時間 3,955 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils

proc IDE*(T : typedesc[int]) : int = return 0
proc PRO*(T : typedesc[int]) : int = return 1


type
  mint[M : static[int]] = object
    val : int

proc inv_mod*(A , M : int) : int =
  var
    a = A
    b = M
    x = 1
    u = 0
  while b > 0:
    var q = a div b
    var tmp = u
    u = x - q * u
    x = tmp
    tmp = b
    b = a - q * b
    a = tmp
  return (A + M) mod M    

proc newMint*[M](val : int) : mint[M] =
  return mint[M](val : (val mod M + M) mod M)

proc `$`*[M](m : mint[M]) : string = return m.val.intToStr
proc `+`*[M](a : mint[M] , b : mint[M]) : mint[M] = return mint[M](val : (a.val + b.val) mod M)
proc `-`*[M](a : mint[M] , b : mint[M]) : mint[M] = return mint[M](val : (a.val - b.val + M) mod M)
proc `*`*[M](a : mint[M] , b : mint[M]) : mint[M] = return mint[M](val : (a.val * b.val) mod M)
proc `/`*[M](a : mint[M] , b : mint[M]) : mint[M] = return mint[M](val : (a.val * inv_mod(b.val , M)) mod M)
proc `+`*[M](a : mint[M] , b : int) : mint[M]     = return mint[M](val : (a.val + b) mod M)
proc `-`*[M](a : mint[M] , b : int) : mint[M]     = return mint[M](val : (a.val - b + M) mod M)
proc `*`*[M](a : mint[M] , b : int) : mint[M]     = return mint[M](val : (a.val * b) mod M)
proc `/`*[M](a : mint[M] , b : int) : mint[M]     = return mint[M](val : (a.val * inv_mod(b , M)) mod M)
proc `+`*[M](a : int , b : mint[M]) : mint[M]     = return mint[M](val : (a + b.val) mod M)
proc `-`*[M](a : int , b : mint[M]) : mint[M]     = return mint[M](val : (a - b.val + M) mod M)
proc `*`*[M](a : int , b : mint[M]) : mint[M]     = return mint[M](val : (a * b.val) mod M)
proc `/`*[M](a : int , b : mint[M]) : mint[M]     = return mint[M](val : (a * inv_mod(b.val , M)) mod M)
proc `^`*[M](a : mint[M] , b : int) : mint[M] =
  var
    ans = newMint[M](1)
    c = a
    r = b
  while r > 0:
    if (r and 1) == 1: ans *= c
    c = c * c
    r = r shr 1
  return ans
type MM = mint[(int)(1e9 + 7)]

proc IDE*(T : typedesc[MM]) : MM = return MM(val : 0)
proc PRO*(T : typedesc[MM]) : MM = return MM(val : 1)



type Matrix*[T] = seq[seq[T]]

proc newMatrix*[T](h , w : int) : Matrix[T] =
    var mat : Matrix[T] = newSeqWith(h,newSeqWith(w , IDE(T)))
    return mat

proc `*`*[T](a , b : Matrix[T]) : Matrix[T] =
    var res = newMatrix[T](a.len , b[0].len)
    for i in 0..<a.len:
        for k in 0..<b.len:
            for j in 0..<b[0].len:
                res[i][j] = res[i][j] + a[i][k] * b[k][j]
    return res

proc `+`*[T](a , b : Matrix[T]) : Matrix[T] =
    var res = a
    for i in 0..<res.len:
        for j in 0..<res[0].len:
            res[i][j] += b[i][j]
    return res


proc `*`*[T](a : Matrix[T] , b : T) : Matrix[T] =
    var res = a
    for i in 0..<res.len:
        for j in 0..<res[0].len:
            res[i][j] += b
    return res

proc `^`*[T](a : Matrix[T] , n : int) : Matrix[T] =
    var 
        A = a
        res = newMatrix[T](a.len , a[0].len)
        r = n
    for i in 0..<res.len: res[i][i] = PRO(T)

    while r > 0:
        if (r and 1) == 1: res = res * A
        A = A * A
        r = r shr 1
    return res

# verify https://yukicoder.me/problems/no/718


import strutils

var
    N = stdin.readline.parseInt
    m = @[@[MM(val : 1) , MM(val : 0)]]
    mm = @[@[MM(val : 1) , MM(val : 1)] , @[MM(val : 1 ,) , MM(val : 0)]]
    a = (m * (mm ^ (N)))
echo a[0][0] * a[0][1]
0