結果
| 問題 |
No.2445 奇行列式
|
| コンテスト | |
| ユーザー |
qwewe
|
| 提出日時 | 2025-04-24 12:22:59 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,876 bytes |
| コンパイル時間 | 319 ms |
| コンパイル使用メモリ | 82,016 KB |
| 実行使用メモリ | 372,448 KB |
| 最終ジャッジ日時 | 2025-04-24 12:25:08 |
| 合計ジャッジ時間 | 6,825 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 13 TLE * 1 -- * 6 |
ソースコード
import sys
from itertools import combinations
def compute_permanent(A, N, mod):
# Precompute masks for each number of set bits
masks = [[] for _ in range(N+1)]
for mask in range(1 << N):
cnt = bin(mask).count('1')
masks[cnt].append(mask)
dp = [0] * (1 << N)
dp[0] = 1 # Base case: empty mask
for i in range(N):
new_dp = [0] * (1 << N)
current_masks = masks[i]
for mask in current_masks:
current_val = dp[mask]
if current_val == 0:
continue
# Iterate over all possible columns j not in mask
for j in range(N):
if not (mask & (1 << j)):
new_mask = mask | (1 << j)
new_dp[new_mask] = (new_dp[new_mask] + current_val * A[i][j]) % mod
dp = new_dp
return dp[(1 << N) - 1]
def compute_determinant(A, N, mod):
# Precompute masks for each number of set bits
masks = [[] for _ in range(N+1)]
for mask in range(1 << N):
cnt = bin(mask).count('1')
masks[cnt].append(mask)
# dp[mask][parity] where parity is 0 (even) or 1 (odd)
dp = [[0] * 2 for _ in range(1 << N)]
dp[0][0] = 1 # Base case: empty mask, even parity
for i in range(N):
new_dp = [[0] * 2 for _ in range(1 << N)]
current_masks = masks[i]
for mask in current_masks:
for parity in 0, 1:
current_val = dp[mask][parity]
if current_val == 0:
continue
# Iterate over all possible columns j not in mask
for j in range(N):
if not (mask & (1 << j)):
# Calculate the number of set bits higher than j
cnt = (mask >> (j + 1)).bit_count()
new_parity = (parity + cnt) % 2
new_mask = mask | (1 << j)
contribution = (current_val * A[i][j]) % mod
new_dp[new_mask][new_parity] = (new_dp[new_mask][new_parity] + contribution) % mod
dp = new_dp
full_mask = (1 << N) - 1
determinant = 0
for parity in 0, 1:
sign = 1 if parity % 2 == 0 else -1
term = (sign * dp[full_mask][parity]) % mod
determinant = (determinant + term) % mod
return determinant
def main():
input = sys.stdin.read().split()
ptr = 0
N = int(input[ptr])
ptr += 1
B = int(input[ptr])
ptr += 1
A = []
for _ in range(N):
row = list(map(int, input[ptr:ptr+N]))
ptr += N
A.append(row)
mod = 2 * B
permanent = compute_permanent(A, N, mod)
determinant = compute_determinant(A, N, mod)
diff = (permanent - determinant) % mod
ans = (diff // 2) % B
print(ans)
if __name__ == "__main__":
main()
qwewe