Submission #65923987


Source Code Expand

import sys
MOD = 998244353

def solve():
    input = sys.stdin.read().split()
    ptr = 0
    T = int(input[ptr])
    ptr += 1
    for _ in range(T):
        N = int(input[ptr])
        K = int(input[ptr + 1])
        ptr += 2
        
        s = bin(N)[2:]
        m = len(s)
        # dp[pos][count][tight] = (sum, cnt)
        dp = {}
        
        def dfs(pos, count, tight):
            if pos == m:
                return (0, 1) if count == K else (0, 0)
            key = (pos, count, tight)
            if key in dp:
                return dp[key]
            
            limit = int(s[pos]) if tight else 1
            total_sum = 0
            total_cnt = 0
            for d in range(0, limit + 1):
                new_tight = tight and (d == limit)
                new_count = count + (1 if d == 1 else 0)
                if new_count > K:
                    continue
                sum_sub, cnt_sub = dfs(pos + 1, new_count, new_tight)
                total_sum += sum_sub
                if d == 1:
                    total_sum += cnt_sub * (1 << (m - 1 - pos))
                total_sum %= MOD
                total_cnt += cnt_sub
                total_cnt %= MOD
            dp[key] = (total_sum, total_cnt)
            return dp[key]
        
        res_sum, res_cnt = dfs(0, 0, True)
        print(res_sum % MOD)

solve()

Submission Info

Submission Time
Task E - Popcount Sum 3
User wyzl
Language Python (PyPy 3.10-v7.3.12)
Score 450
Code Size 1394 Byte
Status AC
Exec Time 163 ms
Memory 91960 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 1
AC × 34
Set Name Test Cases
Sample example_00.txt
All example_00.txt, hand_00.txt, hand_01.txt, hand_02.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 57 ms 80348 KiB
hand_00.txt AC 149 ms 87584 KiB
hand_01.txt AC 57 ms 80024 KiB
hand_02.txt AC 63 ms 82040 KiB
random_00.txt AC 138 ms 86784 KiB
random_01.txt AC 163 ms 91960 KiB
random_02.txt AC 143 ms 86672 KiB
random_03.txt AC 151 ms 88424 KiB
random_04.txt AC 144 ms 86784 KiB
random_05.txt AC 114 ms 83988 KiB
random_06.txt AC 132 ms 85596 KiB
random_07.txt AC 118 ms 85092 KiB
random_08.txt AC 128 ms 87312 KiB
random_09.txt AC 111 ms 84260 KiB
random_10.txt AC 131 ms 87136 KiB
random_11.txt AC 118 ms 85636 KiB
random_12.txt AC 122 ms 85284 KiB
random_13.txt AC 119 ms 84380 KiB
random_14.txt AC 117 ms 84484 KiB
random_15.txt AC 135 ms 88416 KiB
random_16.txt AC 130 ms 86708 KiB
random_17.txt AC 118 ms 84968 KiB
random_18.txt AC 132 ms 88068 KiB
random_19.txt AC 128 ms 86788 KiB
random_20.txt AC 111 ms 84776 KiB
random_21.txt AC 117 ms 84592 KiB
random_22.txt AC 121 ms 85460 KiB
random_23.txt AC 150 ms 89216 KiB
random_24.txt AC 117 ms 84920 KiB
random_25.txt AC 116 ms 84024 KiB
random_26.txt AC 122 ms 86148 KiB
random_27.txt AC 121 ms 84732 KiB
random_28.txt AC 124 ms 86456 KiB
random_29.txt AC 114 ms 84784 KiB