結果

問題 No.1825 Except One
ユーザー gew1fw
提出日時 2025-06-12 13:46:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,098 ms / 3,000 ms
コード長 1,410 bytes
コンパイル時間 194 ms
コンパイル使用メモリ 82,216 KB
実行使用メモリ 286,764 KB
最終ジャッジ日時 2025-06-12 13:46:46
合計ジャッジ時間 11,817 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

def main():
    import sys
    input = sys.stdin.read().split()
    N = int(input[0])
    A = list(map(int, input[1:N+1]))
    
    # Initialize DP: dp[m] is a dictionary of (sum, max_val) -> count
    dp = [defaultdict(int) for _ in range(N+2)]  # m ranges from 0 to N
    dp[0][(0, 0)] = 1  # base case: 0 elements, sum 0, max 0
    
    for a in A:
        # Create a temporary structure to hold new states
        temp = [defaultdict(int) for _ in range(N+2)]
        for m in range(N+1):
            for (s, curr_max), cnt in dp[m].items():
                # Take the current element a
                new_m = m + 1
                if new_m > N:
                    continue
                new_s = s + a
                new_max = max(curr_max, a)
                temp[new_m][(new_s, new_max)] += cnt
        
        # Merge the temp into dp
        for m in range(N+1):
            for (s_max, cnt) in temp[m].items():
                s, max_val = s_max
                dp[m][(s, max_val)] += cnt
    
    ans = 0
    for m in range(2, N+1):
        for (s, max_val), cnt in dp[m].items():
            if (m - 1) == 0:
                continue  # impossible since m >=2
            if s % (m - 1) != 0:
                continue
            k = s // (m - 1)
            if max_val <= k:
                ans += cnt
    print(ans)

if __name__ == '__main__':
    main()
0