結果
| 問題 | No.526 フィボナッチ数列の第N項をMで割った余りを求める | 
| コンテスト | |
| ユーザー |  buey_t | 
| 提出日時 | 2023-03-12 20:04:31 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 141 ms / 2,000 ms | 
| コード長 | 1,850 bytes | 
| コンパイル時間 | 294 ms | 
| コンパイル使用メモリ | 82,888 KB | 
| 実行使用メモリ | 88,932 KB | 
| 最終ジャッジ日時 | 2024-09-18 07:13:16 | 
| 合計ジャッジ時間 | 3,253 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 12 | 
ソースコード
def main():
    try:
        import pypyjit
        pypyjit.set_param('max_unroll_recursion=-1')
    except:
        pass
    try:
        import sys
        sys.setrecursionlimit(10**7)
    except:
        pass
    from math import sqrt,sin,cos,tan,ceil,radians,floor,gcd,exp,log,log10,log2,factorial,fsum
    from heapq import heapify, heappop, heappush
    from bisect import bisect_left, bisect_right
    from copy import deepcopy
    import copy
    import random
    from collections import deque,Counter,defaultdict
    from itertools import permutations,combinations
    from decimal import Decimal,ROUND_HALF_UP
    #tmp = Decimal(mid).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
    from functools import lru_cache, reduce
    #@lru_cache(maxsize=None)
    from operator import add,sub,mul,xor,and_,or_,itemgetter
    INF = 10**18
    mod1 = 10**9+7
    mod2 = 998244353
    
    #DecimalならPython
    
    N,M = map(int, input().split())
    
    def matmul(A,B,mod):
        res = [[0]*len(B[0]) for _ in [None]*len(A)]
        for i, resi in enumerate(res):
            for k, aik in enumerate(A[i]):
                for j,bkj in enumerate(B[k]):
                    resi[j] += aik*bkj
                    resi[j] %= mod
        return res
    
    def matpow(A,p,mod):
        if p%2:
            return matmul(A, matpow(A,p-1,mod),mod)
        elif p > 0:
            b = matpow(A,p//2,mod)
            return matmul(b,b,mod)
        else:
            return [[int(i==j) for j in range(len(A))] for i in range(len(A))]
    
    A = [[1,1],[1,0]]
    
    B = matpow(A,N-2,M)
    
    print(B[0][0]%M)
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
if __name__ == '__main__':
    main()
            
            
            
        