結果

問題 No.613 Solitude by the window
ユーザー yuppe19 😺
提出日時 2017-12-12 23:39:46
言語 Python2
(2.7.18)
結果
WA  
実行時間 -
コード長 869 bytes
コンパイル時間 49 ms
コンパイル使用メモリ 7,040 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-12-14 06:41:29
合計ジャッジ時間 1,156 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14 WA * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/python2
# -*- coding: utf-8 -*-
# †
n, mod = map(int, raw_input().split())
if n > 100:
    p = pow(2, n, mod-1) + mod - 1
else:
    p = 2 ** n


def matmul(A, B):
    p, q = len(A[0]), len(B)
    if p != q:
        raise ValueError('len(A[0])={} len(B)={}'.format(p, q))
    rows, cols, times = len(A), len(B[0]), len(A[0])
    res = [[0] * cols for _ in xrange(rows)]
    for r in xrange(rows):
        for c in xrange(cols):
            res[r][c] = sum(A[r][i]*B[i][c] % mod for i in xrange(times)) % mod
    return res

def matpow(A, n):
    m = len(A)
    res = [[int(i==j) for j in xrange(m)] for i in xrange(m)]
    while n > 0:
        if n & 1:
            res = matmul(res, A)
        A = matmul(A, A)
        n >>= 1
    return res

A = [[2, 3], [1, 2]]
B = matpow(A, p)
C = [[1], [0]]
D = matmul(B, C)
res = 2 * D[0][0] - 2
res %= mod
print res
0