結果

問題 No.2060 AND Sequence
ユーザー lam6er
提出日時 2025-04-09 21:02:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 1,285 bytes
コンパイル時間 433 ms
コンパイル使用メモリ 82,232 KB
実行使用メモリ 61,764 KB
最終ジャッジ日時 2025-04-09 21:05:34
合計ジャッジ時間 3,509 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353

N, M = map(int, input().split())

# Precompute N^c mod MOD for c from 0 to 30
powN = [1] * (31)
for c in range(1, 31):
    powN[c] = (powN[c-1] * N) % MOD

# Compute the binary representation of M as a list of 30 bits (MSB first)
bits = []
for i in range(30):
    bit = (M >> (29 - i)) & 1
    bits.append(bit)

# Initialize the DP table: dp[tight][count]
dp = [[0] * 31 for _ in range(2)]
dp[1][0] = 1  # Start with tight=True and count=0

for i in range(30):
    next_dp = [[0] * 31 for _ in range(2)]
    for tight in [0, 1]:
        for count in range(31):
            if dp[tight][count] == 0:
                continue
            max_bit = bits[i] if tight else 1
            for b in [0, 1]:
                if b > max_bit:
                    continue
                new_tight = 1 if (tight and (b == max_bit)) else 0
                new_count = count + b
                if new_count > 30:
                    continue  # Cannot have more than 30 bits set
                next_dp[new_tight][new_count] = (next_dp[new_tight][new_count] + dp[tight][count]) % MOD
    dp = next_dp

# Sum all possibilities for tight and count
total = 0
for tight in [0, 1]:
    for count in range(31):
        total = (total + dp[tight][count] * powN[count]) % MOD

print(total)
0