結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー dn6049949dn6049949
提出日時 2020-03-29 22:30:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 1,031 bytes
コンパイル時間 675 ms
コンパイル使用メモリ 87,156 KB
実行使用メモリ 71,352 KB
最終ジャッジ日時 2023-08-30 20:01:28
合計ジャッジ時間 2,409 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,168 KB
testcase_01 AC 72 ms
71,020 KB
testcase_02 AC 70 ms
71,248 KB
testcase_03 AC 73 ms
70,820 KB
testcase_04 AC 71 ms
71,352 KB
testcase_05 AC 72 ms
70,908 KB
testcase_06 AC 73 ms
71,184 KB
testcase_07 AC 72 ms
71,332 KB
testcase_08 AC 76 ms
71,168 KB
testcase_09 AC 71 ms
71,060 KB
testcase_10 AC 71 ms
71,288 KB
testcase_11 AC 72 ms
71,084 KB
testcase_12 AC 72 ms
71,080 KB
testcase_13 AC 71 ms
71,228 KB
testcase_14 AC 70 ms
70,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 行列累乗 O(H^3logN) (H := matrix Aの次元)

# 行列演算に用いる演算add,mulおよびmulの単位元
add = lambda x,y:x+y
mul = lambda x,y:x*y
default = 1

# 内積
def product(a, b, mod=None):
    if mod is None:
        res = 0
        for i in map(lambda x:mul(x[0],x[1]), zip(a,b)):
            res = add(res,i)
        return res
    else:
        res = 0
        for i in map(lambda x:mul(x[0],x[1])%mod, zip(a,b)):
            res = add(res,i)%mod
        return res

# 行列積
def mul_of_matrix(a, b, mod=None):
    bt = [[b[i][j] for i in range(len(b))] for j in range(len(b[0]))]
    return [[product(ai,bj,mod) for bj in bt] for ai in a]

# 行列累乗
def pow_of_matrix(a, n, mod=None):
    res = [[default if i == j else 0 for j in range(len(a))] for i in range(len(a))]
    while n:
        if n&1:
            res = mul_of_matrix(res,a,mod)
        a = mul_of_matrix(a,a,mod)
        n >>= 1
    return res

n,m = map(int, input().split())
mat = pow_of_matrix([[1,1],[1,0]],n-1,m)
print(mat[1][0])
0