用DP或DFS
import sysdef dfs(ptr, nowSum): global count if nowSum == half: count += 1 return 1 state = (ptr, nowSum) if state in memo: count += memo[state] return memo[state] remain = ptr * (ptr + 1) // 2 if nowSum > half or ptr <= 0 or nowSum + remain < half: return 0 # 把自己也放進去 temp1 = dfs(ptr-1, nowSum + ptr) # 自己不要放入 temp2 = dfs(ptr-1, nowSum) res = temp1 + temp2 # Result, 暫存結果 memo[state] = res return reswhile True: try: N = int(input()) nums = [i for i in range(N, 0, -1)] # N~1 if (N * (N + 1) // 2) % 2 == 1: # 如果sum是奇數 則不可能分堆 print(0) continue sys.setrecursionlimit(99999) half = (N * (N + 1) // 2) // 2 got = set() count = 0 memo = dict() # 紀錄 (第幾個數字, 目前總和) dfs(N, 0) print(count // 2) except EOFError: sys.exit()