結果

問題 No.2237 Xor Sum Hoge
ユーザー lam6er
提出日時 2025-03-26 15:57:27
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,827 bytes
コンパイル時間 509 ms
コンパイル使用メモリ 82,072 KB
実行使用メモリ 270,852 KB
最終ジャッジ日時 2025-03-26 15:58:28
合計ジャッジ時間 22,795 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other TLE * 1 -- * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353

def main():
    import sys
    N, B, C = map(int, sys.stdin.readline().split())
    
    # Precompute factorial and inverse factorial modulo MOD
    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
    
    cur_dp = {0: 1}
    
    for k in range(60):
        b_k = (B >> k) & 1
        c_k = (C >> k) & 1
        
        next_dp = {}
        
        for c_in, count in cur_dp.items():
            # Check if the condition c_k ≡ (b_k - c_in) mod 2 holds
            if (c_k % 2) != ((b_k - c_in) % 2 + 2) % 2:
                continue
            
            numerator_min = c_in - b_k
            c_out_min = (numerator_min + 1) // 2
            c_out_min = max(c_out_min, 0)
            
            numerator_max = N + c_in - b_k
            c_out_max = numerator_max // 2
            
            if c_out_min > c_out_max:
                continue
            
            for c_out in range(c_out_min, c_out_max + 1):
                s = b_k + 2 * c_out - c_in
                if s < 0 or s > N:
                    continue  # This should not happen due to earlier checks
                c = comb(N, s)
                if c == 0:
                    continue
                next_dp[c_out] = (next_dp.get(c_out, 0) + count * c) % MOD
        
        cur_dp = next_dp
        if not cur_dp:
            break  # No possible way, exit early
    
    print(cur_dp.get(0, 0) % MOD)

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