結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー mkawa2mkawa2
提出日時 2020-01-23 11:10:49
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
MLE  
実行時間 -
コード長 4,158 bytes
コンパイル時間 730 ms
コンパイル使用メモリ 11,492 KB
実行使用メモリ 816,232 KB
最終ジャッジ日時 2023-09-25 21:52:24
合計ジャッジ時間 8,494 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,684 KB
testcase_01 AC 19 ms
8,656 KB
testcase_02 AC 364 ms
9,012 KB
testcase_03 AC 81 ms
8,656 KB
testcase_04 AC 188 ms
8,732 KB
testcase_05 AC 160 ms
8,852 KB
testcase_06 AC 184 ms
8,752 KB
testcase_07 AC 262 ms
8,804 KB
testcase_08 AC 66 ms
8,704 KB
testcase_09 AC 219 ms
8,772 KB
testcase_10 AC 123 ms
8,732 KB
testcase_11 AC 120 ms
8,836 KB
testcase_12 AC 166 ms
8,712 KB
testcase_13 AC 95 ms
8,712 KB
testcase_14 AC 45 ms
8,716 KB
testcase_15 AC 289 ms
8,848 KB
testcase_16 AC 263 ms
8,812 KB
testcase_17 AC 112 ms
8,676 KB
testcase_18 AC 277 ms
8,808 KB
testcase_19 AC 346 ms
8,920 KB
testcase_20 AC 37 ms
8,652 KB
testcase_21 MLE -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

class mint:
    def __init__(self, x):
        self.__x = x % md

    def __str__(self):
        return str(self.__x)

    def __add__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x + other)

    def __sub__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x - other)

    def __rsub__(self, other):
        return mint(other - self.__x)

    def __mul__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x * other)

    __radd__ = __add__
    __rmul__ = __mul__

    def __truediv__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x * pow(other, md - 2, md))

    def __pow__(self, power, modulo=None):
        return mint(pow(self.__x, power, md))

class Fibonacci:
    def __init__(self, aa):
        n = len(aa)
        coff = [1] * n
        self.f0 = aa
        # 上2つは問題ごとに手作業で設定
        # af(n)+bf(n+1)+cf(n+2)+df(n+3)=f(n+4)みたいなとき
        # coff=[a,b,c,d]
        # 初期値f0(f(0)からf(3))
        ff = [[0] * n for _ in range(2 * n - 1)]
        for i in range(n): ff[i][i] = mint(1)
        for i in range(n, 2 * n - 1):
            ffi = ff[i]
            for j, c in enumerate(coff, i - n):
                ffj = ff[j]
                for k in range(n): ffi[k] += c * ffj[k]
        self.bn = 1 << (n - 1).bit_length()
        self.base = ff[self.bn]
        self.ff = ff
        self.n = n

    def __mm(self, aa, bb):
        n = self.n
        res = [0] * (n * 2 - 1)
        for i, a in enumerate(aa):
            for j, b in enumerate(bb):
                res[i + j] += a * b
        for i in range(n, 2 * n - 1):
            c = res[i]
            ffi = self.ff[i]
            for j in range(n):
                res[j] += c * ffi[j]
        return res[:n]

    def v(self, x):
        base = self.base
        aa = self.ff[x % self.bn]
        x //= self.bn
        while x:
            if x & 1: aa = self.__mm(aa, base)
            base = self.__mm(base, base)
            x >>= 1
        return sum(a * f for a, f in zip(aa, self.f0))

class Sibonacci:
    def __init__(self, aa):
        n = len(aa)+1
        coff = [-1]+[0]*(n-2)+[2]
        fb=Fibonacci(aa)
        f0=[fb.v(0)]
        for x in range(1,n):
            f0.append(f0[-1]+fb.v(x))
        self.f0 = f0
        # 上2つは問題ごとに手作業で設定
        # af(n)+bf(n+1)+cf(n+2)+df(n+3)=f(n+4)みたいなとき
        # coff=[a,b,c,d]
        # 初期値f0(f(0)からf(3))
        ff = [[0] * n for _ in range(2 * n - 1)]
        for i in range(n):ff[i][i] = mint(1)
        for i in range(n, 2 * n - 1):
            ffi = ff[i]
            for j, c in enumerate(coff, i - n):
                ffj = ff[j]
                for k in range(n): ffi[k] += c * ffj[k]
        self.bn = 1 << (n - 1).bit_length()
        self.base = ff[self.bn]
        self.ff = ff
        self.n = n

    def __mm(self, aa, bb):
        n = self.n
        res = [0] * (n * 2 - 1)
        for i, a in enumerate(aa):
            for j, b in enumerate(bb):
                res[i + j] += a * b
        for i in range(n, 2 * n - 1):
            c = res[i]
            ffi = self.ff[i]
            for j in range(n):
                res[j] += c * ffi[j]
        return res[:n]

    def v(self, x):
        base = self.base
        aa = self.ff[x % self.bn]
        x //= self.bn
        while x:
            if x & 1: aa = self.__mm(aa, base)
            base = self.__mm(base, base)
            x >>= 1
        return sum(a * f for a, f in zip(aa, self.f0))

md=10**9+7
def main():
    n,k=MI()
    f0=LI()
    s=Sibonacci(f0)
    print(s.v(k-1)-s.v(k-2),s.v(k-1))

main()
0