結果

問題 No.2062 Sum of Subset mod 999630629
ユーザー gew1fw
提出日時 2025-06-12 12:56:27
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,235 bytes
コンパイル時間 346 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 82,560 KB
最終ジャッジ日時 2025-06-12 13:02:49
合計ジャッジ時間 3,918 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21 WA * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353
m = 999630629

def main():
    import sys
    input = sys.stdin.read().split()
    N = int(input[0])
    A = list(map(int, input[1:N+1]))
    sum_A = sum(A)
    
    if sum_A < m:
        ans = sum_A * pow(2, N-1, MOD) % MOD
        print(ans)
        return
    
    # Precompute factorial and inverse factorial modulo MOD up to max_n = N
    max_n = N
    fact = [1] * (max_n + 1)
    for i in range(1, max_n + 1):
        fact[i] = fact[i-1] * i % MOD
    inv_fact = [1] * (max_n + 1)
    inv_fact[max_n] = pow(fact[max_n], MOD-2, MOD)
    for i in range(max_n-1, -1, -1):
        inv_fact[i] = inv_fact[i+1] * (i+1) % MOD
    
    def comb(n, k):
        if k < 0 or k > n:
            return 0
        return fact[n] * inv_fact[k] % MOD * inv_fact[n - k] % MOD
    
    max_t = (sum_A - m) // 10**4  # 36 when sum_A=1e9, m=999630629
    max_t = min(max_t, 36)
    
    C = 0
    for t in range(0, max_t + 1):
        c = comb(N, t)
        C = (C + c) % MOD
    
    term1 = sum_A % MOD
    term1 = term1 * pow(2, N-1, MOD) % MOD
    term2 = m % MOD
    term2 = term2 * C % MOD
    ans = (term1 - term2) % MOD
    ans = (ans + MOD) % MOD  # Ensure non-negative
    print(ans)

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