結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー vwxyzvwxyz
提出日時 2021-08-20 21:49:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 158 ms / 2,000 ms
コード長 4,023 bytes
コンパイル時間 187 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 89,472 KB
最終ジャッジ日時 2024-10-14 03:17:37
合計ジャッジ時間 4,711 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 156 ms
89,472 KB
testcase_01 AC 154 ms
89,344 KB
testcase_02 AC 158 ms
89,216 KB
testcase_03 AC 152 ms
89,472 KB
testcase_04 AC 153 ms
89,472 KB
testcase_05 AC 153 ms
89,216 KB
testcase_06 AC 154 ms
89,472 KB
testcase_07 AC 155 ms
89,472 KB
testcase_08 AC 153 ms
89,472 KB
testcase_09 AC 153 ms
89,216 KB
testcase_10 AC 151 ms
89,472 KB
testcase_11 AC 152 ms
89,472 KB
testcase_12 AC 153 ms
89,216 KB
testcase_13 AC 154 ms
89,344 KB
testcase_14 AC 153 ms
89,216 KB
testcase_15 AC 150 ms
89,472 KB
testcase_16 AC 152 ms
89,472 KB
testcase_17 AC 153 ms
89,472 KB
testcase_18 AC 154 ms
89,344 KB
testcase_19 AC 154 ms
89,344 KB
testcase_20 AC 153 ms
89,216 KB
testcase_21 AC 153 ms
89,472 KB
testcase_22 AC 152 ms
89,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
import copy
import decimal
import fractions
import functools
import heapq
import itertools
import math
import random
import sys
from collections import Counter,deque,defaultdict
from functools import lru_cache,reduce
from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max
def _heappush_max(heap,item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappushpop_max(heap, item):
    if heap and item < heap[0]:
        item, heap[0] = heap[0], item
        heapq._siftup_max(heap, 0)
    return item
from math import degrees, gcd as GCD
read=sys.stdin.read
readline=sys.stdin.readline
readlines=sys.stdin.readlines


def FFT(polynomial1,polynomial2,digit=10**5):
    def DFT(polynomial,n,inverse=False):
        N=len(polynomial)
        if inverse:
            primitive_root=[math.cos(-i*2*math.pi/(1<<n))+math.sin(-i*2*math.pi/(1<<n))*1j for i in range(1<<n)]
        else:
            primitive_root=[math.cos(i*2*math.pi/(1<<n))+math.sin(i*2*math.pi/(1<<n))*1j for i in range(1<<n)]
        dft=polynomial+[0]*((1<<n)-N)
        if inverse:
            for bit in range(1,n+1):
                a=1<<bit-1
                for i in range(1<<n-bit):
                    for j in range(a):
                        s=i*2*a+j
                        t=s+a
                        dft[s],dft[t]=dft[s]+dft[t]*primitive_root[j<<n-bit],dft[s]-dft[t]*primitive_root[j<<n-bit]
        else:
            for bit in range(n,0,-1):
                a=1<<bit-1
                for i in range(1<<n-bit):
                    for j in range(a):
                        s=i*2*a+j
                        t=s+a
                        dft[s],dft[t]=dft[s]+dft[t],primitive_root[j<<n-bit]*(dft[s]-dft[t])
        return dft
    
    def FFT_(polynomial1,polynomial2):
        N1=len(polynomial1)
        N2=len(polynomial2)
        N=N1+N2-1
        n=(N-1).bit_length()
        fft=[x*y for x,y in zip(DFT(polynomial1,n),DFT(polynomial2,n))]
        fft=DFT(fft,n,inverse=True)
        fft=[round((fft[i]/(1<<n)).real) for i in range(N)]
        return fft
    
    N1=len(polynomial1)
    N2=len(polynomial2)
    N=N1+N2-1
    polynomial11,polynomial12=[None]*N1,[None]*N1
    polynomial21,polynomial22=[None]*N2,[None]*N2
    for i in range(N1):
        polynomial11[i],polynomial12[i]=divmod(polynomial1[i],digit)
    for i in range(N2):
        polynomial21[i],polynomial22[i]=divmod(polynomial2[i],digit)
    polynomial=[0]*(N)
    a=digit**2-digit
    for i,x in enumerate(FFT_(polynomial11,polynomial21)):
        polynomial[i]+=x*a
    a=digit-1
    for i,x in enumerate(FFT_(polynomial12,polynomial22)):
        polynomial[i]-=x*a
    for i,x in enumerate(FFT_([x1+x2 for x1,x2 in zip(polynomial11,polynomial12)],[x1+x2 for x1,x2 in zip(polynomial21,polynomial22)])):
        polynomial[i]+=x*digit
    return polynomial

def Bostan_Mori(poly_deno,poly_nume,N,mod=0,fft=False,ntt=False):
    if ntt:
        convolve=NTT
    elif fft:
        convolve=FFT
    else:
        def convolve(poly_deno,poly_nume):
            conv=[0]*(len(poly_deno)+len(poly_nume)-1)
            for i in range(len(poly_deno)):
                for j in range(len(poly_nume)):
                    conv[i+j]+=poly_deno[i]*poly_nume[j]
            if mod:
                for i in range(len(conv)):
                    conv[i]%=mod
            return conv
    while N:
        poly_nume_=[-x if i%2 else x for i,x in enumerate(poly_nume)]
        if N%2:
            poly_deno=convolve(poly_deno,poly_nume_)[1::2]
        else:
            poly_deno=convolve(poly_deno,poly_nume_)[::2]
        poly_nume=convolve(poly_nume,poly_nume_)[::2]
        if fft and mod:
            for i in range(len(poly_deno)):
                poly_deno[i]%=mod
            for i in range(len(poly_nume)):
                poly_nume[i]%=mod
        N//=2
    return poly_deno[0]

N=int(readline())
mod=10**9+7
ans=Bostan_Mori([0,1],[1,-1,-1],N,mod=mod)*Bostan_Mori([0,1],[1,-1,-1],N+1,mod=mod)%mod
print(ans)
0