結果

問題 No.506 限られたジャパリまん
ユーザー rlangevin
提出日時 2023-09-27 12:26:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 149 ms / 2,000 ms
コード長 1,192 bytes
コンパイル時間 251 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 76,672 KB
最終ジャッジ日時 2024-07-20 06:33:24
合計ジャッジ時間 3,135 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

def popcount(n):
    cnt = 0
    while n:
        cnt += n & 1
        n >>= 1
    return cnt


H, W, K, P = map(int, input().split())
mod = 10 ** 9 + 7
L = []
G = [[1] * (W + 1) for i in range(H + 1)]
for i in range(K):
    lst = input().split()
    lst[0] = int(lst[0])
    lst[1] = int(lst[1])
    G[lst[0]][lst[1]] = 0
    lst.append(i)
    L.append(lst)

K2 = 1 << K
maxv = 0
name = None

def calc():
    dp = [[0] * (W + 1) for i in range(H + 1)]
    dp[0][0] = 1
    for i in range(H + 1):
        for j in range(W + 1):
            if i == j == 0 or G[i][j] == 0:
                continue
            if i:
                dp[i][j] += dp[i - 1][j]
            if j:
                dp[i][j] += dp[i][j - 1]
    return dp[H][W]



for s in range(K2):
    if popcount(s) != P:
        continue
    for x, y, _, ind in L:
        if (s >> ind) & 1:
            G[x][y] = 1

    temp = calc()
    if temp > maxv:
        maxv = temp
        name = []
        for x, y, n, ind in L:
            if (s >> ind) & 1:
                name.append(n)

    for x, y, _, ind in L:
        if (s >> ind) & 1:
            G[x][y] = 0
    
print(maxv%mod)
if name:
    for n in name:
        print(n)
0