結果
問題 | No.718 行列のできるフィボナッチ数列道場 (1) |
ユーザー | むらため |
提出日時 | 2019-01-27 03:14:02 |
言語 | Nim (2.0.2) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 3,743 bytes |
コンパイル時間 | 2,619 ms |
コンパイル使用メモリ | 60,836 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-01 11:00:06 |
合計ジャッジ時間 | 3,109 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,944 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,944 KB |
testcase_08 | AC | 1 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,940 KB |
testcase_10 | AC | 1 ms
6,940 KB |
testcase_11 | AC | 1 ms
6,944 KB |
testcase_12 | AC | 1 ms
6,940 KB |
testcase_13 | AC | 2 ms
6,940 KB |
testcase_14 | AC | 1 ms
6,940 KB |
testcase_15 | AC | 2 ms
6,940 KB |
testcase_16 | AC | 2 ms
6,944 KB |
testcase_17 | AC | 2 ms
6,944 KB |
testcase_18 | AC | 1 ms
6,944 KB |
testcase_19 | AC | 1 ms
6,940 KB |
testcase_20 | AC | 2 ms
6,944 KB |
testcase_21 | AC | 1 ms
6,944 KB |
testcase_22 | AC | 1 ms
6,940 KB |
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 8) Warning: imported and not used: 'sequtils' [UnusedImport] /home/judge/data/code/Main.nim(1, 17) Warning: imported and not used: 'math' [UnusedImport]
ソースコード
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 # mod # template useModulo = const MOD = 1000000007 type ModInt = object v:int # 0~MODに収まる proc toModInt*(a:int) : ModInt = if a < -MOD : result.v = ((a mod MOD) + MOD) mod MOD elif a < 0 : result.v = a + MOD elif a >= MOD: result.v = a mod MOD else: result.v = a proc `+`*(a,b:ModInt) : ModInt = result.v = a.v + b.v if result.v >= MOD : result.v = result.v mod MOD proc `*`*(a,b:ModInt) : ModInt = result.v = a.v * b.v if result.v >= MOD : result.v = result.v mod MOD proc `^`*(a:ModInt,b:int) : ModInt = if b == 0 : return 1.toModInt() if b == 1 : return a let pow = a^(b div 2) if b mod 2 == 0 : return pow * pow return pow * pow * a proc `+`*(a:int,b:ModInt) : ModInt = a.toModInt() + b proc `+`*(a:ModInt,b:int) : ModInt = a + b.toModInt() proc `-`*(a:ModInt,b:int) : ModInt = a + (-b) proc `-`*(a,b:ModInt) : ModInt = a + (-b.v) proc `-`*(a:int,b:ModInt) : ModInt = a.toModInt() + (-b.v) proc `*`*(a:int,b:ModInt) : ModInt = a.toModInt() * b proc `*`*(a:ModInt,b:int) : ModInt = a * b.toModInt() proc `/`*(a,b:ModInt) : ModInt = a * b^(MOD-2) proc `$`*(a:ModInt) : string = $a.v # useModulo() proc calcFib(n:int) : tuple[x:ModInt,y:ModInt] = if n == 0 : return (0.toModInt(),1.toModInt()) let (fn,fn1) = calcFib(n div 2) let fnx1 = fn1 - fn let f2n = (fn1 + fnx1) * fn let f2nx1 = fn * fn + fnx1 * fnx1 let f2n1 = f2n + f2nx1 if n mod 2 == 0 : return (f2n,f2n1) else: return (f2n1,f2n1 + f2n) let n = scan() echo calcFib(n)[0] * calcFib(n+1)[0]