結果

問題 No.685 Logical Operations
ユーザー lam6er
提出日時 2025-03-20 21:16:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,919 bytes
コンパイル時間 237 ms
コンパイル使用メモリ 82,364 KB
実行使用メモリ 54,184 KB
最終ジャッジ日時 2025-03-20 21:16:56
合計ジャッジ時間 2,108 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

MOD = 10**9 + 7
def main():
N = int(input().strip())
max_k = 60
pow3 = [1] * (max_k + 1)
for i in range(1, max_k + 1):
pow3[i] = (pow3[i-1] * 3) % MOD
def pow_mod(a, b):
res = 1
while b > 0:
if b % 2 == 1:
res = res * a % MOD
a = a * a % MOD
b //= 2
return res
def compute_sum(m, k):
s = bin(m)[2:]
bits = list(map(int, s.zfill(k)))
L = len(bits)
memo = {}
def dfs(pos, tight, cnt):
if pos == L:
return pow(2, k - cnt, MOD)
key = (pos, tight, cnt)
if key in memo:
return memo[key]
res = 0
limit = bits[pos] if tight else 1
for b in range(0, limit + 1):
new_tight = tight and (b == limit)
new_cnt = cnt + (b == 1)
if new_tight:
res += dfs(pos + 1, new_tight, new_cnt)
else:
remaining = L - pos - 1
term = pow(3, remaining, MOD) * pow(2, k - new_cnt - remaining, MOD) % MOD
res += term
res %= MOD
memo[key] = res % MOD
return res % MOD
total = dfs(0, True, 0)
return total % MOD
ans = 0
for k in range(max_k + 1):
y_min = 1 << k
if y_min > N:
continue
d_max = N - y_min
if d_max < 0:
continue
if (1 << k) - 1 <= d_max:
sum_val = pow3[k]
possible_d = (1 << k)
else:
sum_val = compute_sum(d_max, k)
possible_d = d_max + 1
term = (possible_d % MOD) * pow_mod(2, k) % MOD
term = (term - sum_val) % MOD
ans = (ans + term) % MOD
print(ans % MOD)
if __name__ == '__main__':
main()
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0