結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー 6soukiti296soukiti29
提出日時 2017-06-26 23:53:36
言語 Nim
(2.0.2)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,102 bytes
コンパイル時間 4,549 ms
コンパイル使用メモリ 68,940 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-12 13:11:32
合計ジャッジ時間 3,965 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 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,384 KB
testcase_14 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import strutils,sequtils
var m : int64

type
    Matrix[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 : int64
    for i in 1.. Ha:
        for j in 1..Wb:
            k = 0
            for ai in 1..Wa:
                k = (k + a[i][ai] * b[ai][j]) mod m
            result[i][j] = k
proc powMatrix[W,H](a : Matrix[W,H], n : int64):Matrix[W,H] =
    if n == 1:
        return a
    if n mod 2 == 0:
        return powMatrix(a * a,n div 2)
    else:
        return powMatrix(a * a,n div 2) * a
proc printMatrix(A: Matrix)=
    for a in A:
        echo a.join(", ")
proc fib(n : int64): int64 =
    if n == 0:
        return 0
    var
        A : Matrix[2,2] = [[1.int64,1.int64],[1.int64,0.int64]]
    return (powMatrix(A,n))[2][1]
var
    N,ans : int64
    L : seq[int64]
L = stdin.readline.split.map(parseBiggestInt)
(N, m) = (L[0],L[1])
ans = fib(N - 1)
echo ans
0