defsolve(): n = int(input()) A = list(map(int, input().split())) A.sort() assertlen(A) == n
mx = A[-1] # 最大值,決定 DP 陣列大小 f = [1] + [0] * mx # f[j] = 和恰好為 j 的子集數 ans = 0 for i, x inenumerate(A, start=1): if i >= 3: # 至少需要 3 根木棍 ans += 1 << (i - 1) # 前 i-1 根的所有子集 ans %= MOD for j inrange(x + 1): # 減去其餘和 ≤ x 的不合法方案 ans -= f[j] ans %= MOD for j inrange(mx, x - 1, -1): # 0/1 背包,倒序更新 f[j] += f[j - x] f[j] %= MOD print(ans)
if __name__ == "__main__": solve()
寫在最後
Cover Image Credit
The cover image was created by @4AUB. All rights belong to the original artist.
It is used here only as a non-commercial cover illustration for this note. I do not claim ownership of the artwork.
If you are the copyright holder and believe this usage is inappropriate, please contact me by email or leave a comment. I will remove the image promptly.