結果
| 問題 | 
                            No.613 Solitude by the window
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2017-12-12 23:36:52 | 
| 言語 | Python2  (2.7.18)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 892 bytes | 
| コンパイル時間 | 56 ms | 
| コンパイル使用メモリ | 6,912 KB | 
| 実行使用メモリ | 6,272 KB | 
| 最終ジャッジ日時 | 2024-12-14 06:41:22 | 
| 合計ジャッジ時間 | 1,134 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 14 WA * 7 | 
ソースコード
#!/usr/bin/python2
# -*- coding: utf-8 -*-
# †
n, mod = map(int, raw_input().split())
##if 2 ** n >= mod - 1:
if n > 30:
    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