結果
| 問題 | No.526 フィボナッチ数列の第N項をMで割った余りを求める | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2021-02-28 17:45:42 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 54 ms / 2,000 ms | 
| コード長 | 1,996 bytes | 
| コンパイル時間 | 253 ms | 
| コンパイル使用メモリ | 82,528 KB | 
| 実行使用メモリ | 62,080 KB | 
| 最終ジャッジ日時 | 2024-10-02 20:57:58 | 
| 合計ジャッジ時間 | 1,720 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 12 | 
ソースコード
import sys
import math
import itertools
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
from operator import itemgetter, attrgetter
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
def TL(mylist): return [list(x) for x in zip(*mylist)] #行と列入れ替え
def RtoL(mylist): return [list(reversed(x)) for x in mylist] #左右反転
def HtoL(mylist): return [x for x in list(reversed(mylist))] #上下反転
def convert_2d(l, colstart, colend, rawstart, rawend):return [i[rawstart:rawend] for i in l[colstart:colend]] #2次元行列から一部を採取
def get_unique_list(seq):
    seen = []
    return [x for x in seq if x not in seen and not seen.append(x)]
# (an+3, an+2) = (2, 1, 1, 1)(an+1, an)
N, M = MI()
def mat_mul(a, b) :
    I, J, K = len(a), len(b[0]), len(b)
    c = [[0] * J for _ in range(I)]
    for i in range(I) :
        for j in range(J) :
            for k in range(K) :
                c[i][j] += a[i][k] * b[k][j]
            c[i][j] %= M
    return c
def mat_pow(x, n):
    y = [[0] * len(x) for _ in range(len(x))]
    for i in range(len(x)):
        y[i][i] = 1
    while n > 0:
        if n & 1:
            y = mat_mul(x, y)
        x = mat_mul(x, x)
        n >>= 1
    return y
def main():
    N2 = (N-1) // 2
    X =[[2,1],[1,1]]
    Y = mat_pow(X, N2)
    a1 = 0
    a2 = 1
    if N % 2 == 0:
        print(Y[0][0]*a2+Y[0][1]*a1)
    else:
        print(Y[1][0]*a2+Y[1][1]*a1)
    
if __name__ == "__main__":
    main()
            
            
            
        