結果

問題 No.1879 How many matchings?
ユーザー 👑 rin204rin204
提出日時 2022-03-18 22:00:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 1,286 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 82,760 KB
実行使用メモリ 67,056 KB
最終ジャッジ日時 2024-04-14 09:49:40
合計ジャッジ時間 1,734 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
54,480 KB
testcase_01 AC 43 ms
55,192 KB
testcase_02 AC 42 ms
54,416 KB
testcase_03 AC 42 ms
54,752 KB
testcase_04 AC 42 ms
54,524 KB
testcase_05 AC 42 ms
54,388 KB
testcase_06 AC 43 ms
54,940 KB
testcase_07 AC 42 ms
55,168 KB
testcase_08 AC 43 ms
54,736 KB
testcase_09 AC 43 ms
55,860 KB
testcase_10 AC 55 ms
66,176 KB
testcase_11 AC 56 ms
65,744 KB
testcase_12 AC 55 ms
67,056 KB
testcase_13 AC 43 ms
55,364 KB
testcase_14 AC 57 ms
66,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from copy import deepcopy
MOD = 10 ** 9 + 7
    
n = int(input())
if n % 2 == 0:
    n //= 2
    A = [[0, 1], [1, 1]]
    B = [0, 1]
    while n:
        if n & 1:
            C = [0, 0]
            for i in range(2):
                for j in range(2):
                    C[i] += A[i][j] * B[j]
                    C[i] %= MOD
            B = C[:]
        n >>= 1
        C = [[0, 0], [0, 0]]
        for i in range(2):
            for j in range(2):
                for k in range(2):
                    C[i][j] += A[i][k] * A[k][j]
                    C[i][j] %= MOD
        A = deepcopy(C)
    print(B[1])
else:
    A = [[0, 0, 0, 1],
         [1, 0, 1, 0],
         [2, 1, 1, 0],
         [1, 0, 0, 1]]
    B = [0, 0, 0, 1]
    n //= 2
    while n:
        if n & 1:
            C = [0] * 4
            for i in range(4):
                for j in range(4):
                    C[i] += A[i][j] * B[j]
                    C[i] %= MOD
            B = C[:]
        n >>= 1
        C = [[0] * 4 for _ in range(4)]
        for i in range(4):
            for j in range(4):
                for k in range(4):
                    C[i][j] += A[i][k] * A[k][j]
                    C[i][j] %= MOD
        A = deepcopy(C)
    
    ans = B[0] * 2 + B[1] + B[2] + B[3]
    print(ans % MOD)
    
0