結果

問題 No.2846 Birthday Cake
ユーザー 👑 rin204rin204
提出日時 2024-08-23 22:11:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 648 ms / 2,000 ms
コード長 1,112 bytes
コンパイル時間 267 ms
コンパイル使用メモリ 82,344 KB
実行使用メモリ 363,604 KB
最終ジャッジ日時 2024-08-23 22:11:51
合計ジャッジ時間 10,515 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import lcm

k, n = map(int, input().split())
if n == k or k == 1:
    print(1)
    exit()
nCk = [[0] * (n + 1) for _ in range(n + 1)]
nCk[0][0] = 1
for i in range(1, n + 1):
    nCk[i][0] = 1
    for j in range(1, n + 1):
        nCk[i][j] = nCk[i - 1][j - 1] + nCk[i - 1][j]

ng = [False] * 25
ng[13] = True
ng[17] = True
ng[19] = True
ng[23] = True

lc = 1
for i in range(1, n + 1):
    if ng[i]:
        continue
    lc = lcm(lc, i)

dp = [[-1] * (lc + 1) for _ in range((n + 1) * (n + 1) + n + 1)]


def dfs(t, x, tot):
    if tot == lc:
        global ans
        if x == 0:
            return 1
        return 0
    if t == 0:
        return 0

    if ng[t]:
        return dfs(t - 1, x, tot)

    if tot + (lc // t) * x > lc:
        return 0

    if dp[t * (n + 1) + x][tot] != -1:
        return dp[t * (n + 1) + x][tot]

    ret = 0
    for j in range(x + 1):
        ntot = tot + (lc // t) * j
        if ntot > lc:
            break
        ret += dfs(t - 1, x - j, ntot) * nCk[x][j]

    dp[t * (n + 1) + x][tot] = ret
    return ret


ans = dfs(n, k, 0)
if ng[k]:
    ans += 1
print(ans)
0