結果
問題 | No.526 フィボナッチ数列の第N項をMで割った余りを求める |
ユーザー |
![]() |
提出日時 | 2017-06-26 23:53:36 |
言語 | Nim (2.2.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,102 bytes |
コンパイル時間 | 4,095 ms |
コンパイル使用メモリ | 66,196 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-30 01:30:55 |
合計ジャッジ時間 | 4,060 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
import strutils,sequtilsvar m : int64typeMatrix[H,W: static[int]] =array[1..H,array[1..W,int64]]proc `+`[H,W](a,b:Matrix[H,W]):Matrix[H,W] =for i in 1..high(a):for j in 1..high(a[0]):result[i][j] = a[i][j] + b[i][j]proc `*`[Ha,Wa,Wb](a:Matrix[Ha,Wa],b:Matrix[Wa,Wb]):Matrix[Ha,Wb] =var k : int64for i in 1.. Ha:for j in 1..Wb:k = 0for ai in 1..Wa:k = (k + a[i][ai] * b[ai][j]) mod mresult[i][j] = kproc powMatrix[W,H](a : Matrix[W,H], n : int64):Matrix[W,H] =if n == 1:return aif n mod 2 == 0:return powMatrix(a * a,n div 2)else:return powMatrix(a * a,n div 2) * aproc printMatrix(A: Matrix)=for a in A:echo a.join(", ")proc fib(n : int64): int64 =if n == 0:return 0varA : Matrix[2,2] = [[1.int64,1.int64],[1.int64,0.int64]]return (powMatrix(A,n))[2][1]varN,ans : int64L : seq[int64]L = stdin.readline.split.map(parseBiggestInt)(N, m) = (L[0],L[1])ans = fib(N - 1)echo ans