結果

問題 No.1879 How many matchings?
ユーザー shotoyooshotoyoo
提出日時 2022-01-13 00:13:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 82 ms / 2,000 ms
コード長 1,987 bytes
コンパイル時間 429 ms
コンパイル使用メモリ 86,904 KB
実行使用メモリ 76,552 KB
最終ジャッジ日時 2023-08-09 20:30:56
合計ジャッジ時間 2,453 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
75,660 KB
testcase_01 AC 70 ms
71,428 KB
testcase_02 AC 81 ms
76,236 KB
testcase_03 AC 68 ms
71,492 KB
testcase_04 AC 75 ms
76,120 KB
testcase_05 AC 69 ms
71,208 KB
testcase_06 AC 75 ms
76,052 KB
testcase_07 AC 72 ms
71,308 KB
testcase_08 AC 79 ms
76,340 KB
testcase_09 AC 80 ms
76,316 KB
testcase_10 AC 82 ms
76,284 KB
testcase_11 AC 80 ms
76,064 KB
testcase_12 AC 79 ms
76,160 KB
testcase_13 AC 78 ms
76,164 KB
testcase_14 AC 82 ms
76,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

write = lambda x: sys.stdout.write(x+"\n"); writef = lambda x: print("{:.12f}".format(x))
debug = lambda x: sys.stderr.write(x+"\n")
YES="Yes"; NO="No"; pans = lambda v: print(YES if v else NO)
LI = lambda : list(map(int, input().split()))
# sys.setrecursionlimit(3*10**5+10)

### 行列積 matrix multiplication
### 繰り返し2乗法 kurikae
def mul(a,b):
    """行列a,bの積
    aかbがNone のとき、単位行列として扱う
    """
    if a is None:
        return b
    elif b is None:
        return a
    n,m = len(a), len(a[0])
    k,l = len(b), len(b[0])
    # k==m
    out = [[0]*l for _ in range(n)]
    for i in range(n):
        for j in range(l):
            for p in range(m):
                out[i][j] += a[i][p]*b[p][j]%M
                if out[i][j]>M:
                    out[i][j] -= M
    return out
def mul2(m,a):
    """行列と配列の積
    m : None のとき単位行列扱い
    """
    if m is None:
        return a
    k,l = len(m), len(m[0])
#     ll = len(a)
    ans = [0]*k
    for i in range(k):
        for j in range(l):
            ans[i] += m[i][j] * a[j] % M
            if ans[i]>M:
                ans[i] -= M
    return ans
def mulk(m, k):
    """a^kを求める
    """
    if k==0:
        return None
    ans = None
    tmp = m
    while k>0:
        if k&1:
            ans = mul(ans, tmp)
        tmp = mul(tmp, tmp)
        k = k>>1
    return ans

M = 10**9+7
n = int(input())
a = [[0]*6 for _ in range(6)]
for i in range(4):
    a[i][i+2] = 1
a[4][1] = a[4][2] = a[5][4] = 1
if n%2==0:
    b = [1,0,1,1,1,0]
    ak = mulk(a, n)
    res = mul2(ak, b)
    ans = res[0]
else:
    aa = [[0]*12 for _ in range(12)]
    for i in range(6):
        for j in range(6):
            aa[i+6][j+6] = a[i][j]
            aa[i][j] = a[i][j]
    aa[10][3] = aa[10][4] = 1
    b = [1,0,1,1,1,0, 0,0,1,0,0,1]
    ak = mulk(aa, n)
    res = mul2(ak, b)
    ans = res[6]
print(ans%M)
0