結果

問題 No.720 行列のできるフィボナッチ数列道場 (2)
ユーザー realDivineJKrealDivineJK
提出日時 2020-05-26 02:50:39
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 2,173 bytes
コンパイル時間 73 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-04-21 04:03:40
合計ジャッジ時間 1,628 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
11,136 KB
testcase_01 AC 30 ms
11,008 KB
testcase_02 AC 29 ms
11,008 KB
testcase_03 AC 29 ms
11,136 KB
testcase_04 AC 30 ms
11,008 KB
testcase_05 AC 28 ms
11,008 KB
testcase_06 AC 28 ms
11,136 KB
testcase_07 AC 27 ms
11,136 KB
testcase_08 AC 27 ms
11,136 KB
testcase_09 AC 28 ms
11,136 KB
testcase_10 AC 29 ms
11,136 KB
testcase_11 AC 28 ms
11,136 KB
testcase_12 AC 29 ms
11,008 KB
testcase_13 AC 29 ms
11,136 KB
testcase_14 AC 29 ms
11,008 KB
testcase_15 AC 29 ms
11,136 KB
testcase_16 AC 29 ms
11,008 KB
testcase_17 AC 28 ms
11,136 KB
testcase_18 AC 28 ms
11,008 KB
testcase_19 AC 28 ms
11,136 KB
testcase_20 AC 27 ms
11,136 KB
testcase_21 AC 29 ms
10,880 KB
testcase_22 AC 27 ms
11,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M = map(int, input().split())
mod = int(1e9) + 7
maxf = 0 # <-- input factional limitation

def doubling(n, m):
    y = 1
    base = n
    tmp = m
    while tmp != 0:
        if tmp % 2 == 1:
            y *= base
            y %= mod
        base *= base
        base %= mod
        tmp //= 2
    return y

def inved(a):
    x, y, u, v, k, l = 1, 0, 0, 1, a, mod
    while l != 0:
        x, y, u, v = u, v, x - u * (k // l), y - v * (k // l)
        k, l = l, k % l
    return x % mod

fact = [1 for _ in range(maxf+1)]
invf = [1 for _ in range(maxf+1)]

for i in range(maxf):
    fact[i+1] = (fact[i] * (i+1)) % mod
invf[-1] = inved(fact[-1])
for i in range(maxf, 0, -1):
    invf[i-1] = (invf[i] * i) % mod
choice = [M, 2*M, (N+1)*M, (N+2)*M]
temper = [[0 for _ in range(4)], [0 for _ in range(4)]]
for zone in range(4):
    vec1 = [0, 1]
    vec2 = [2, 1]
    tmp = choice[zone]
    mat = [[1, 0], [0, 1]]
    bas = [[0, 1], [1, 1]]
    while tmp != 0:
        y = [[0, 0], [0, 0]]
        if tmp % 2 == 1:
            for i in range(2):
                for j in range(2):
                    for k in range(2):
                        y[i][j] += mat[i][k] * bas[k][j] % mod
                        y[i][j] %= mod
            for i in range(2):
                for j in range(2):
                    mat[i][j] = y[i][j]
        for i in range(2):
            for j in range(2):
                y[i][j] = 0
        for i in range(2):
            for j in range(2):
                for k in range(2):
                    y[i][j] += bas[i][k] * bas[k][j] % mod
                    y[i][j] %= mod
        for i in range(2):
            for j in range(2):
                bas[i][j] = y[i][j]
        tmp //= 2
    vec1[0], vec1[1] = (mat[0][0] * vec1[0] + mat[0][1] * vec1[1]) % mod, (mat[1][0] * vec1[0] + mat[1][1] * vec1[1]) % mod
    vec2[0], vec2[1] = (mat[0][0] * vec2[0] + mat[0][1] * vec2[1]) % mod, (mat[1][0] * vec2[0] + mat[1][1] * vec2[1]) % mod
    temper[0][zone], temper[1][zone] = vec1[0], vec2[0]
S = (temper[0][3] - (temper[1][0] - 1) * (temper[0][2] - temper[0][0]) - temper[0][1]) % mod
S *= inved((temper[1][0] - 2 * (M%2==0))%mod)
S %= mod
print(S)
0