結果

問題 No.2939 Sigma Popcount Problem
ユーザー kemuniku
提出日時 2024-10-18 22:49:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 541 ms / 2,000 ms
コード長 585 bytes
コンパイル時間 271 ms
コンパイル使用メモリ 82,288 KB
実行使用メモリ 76,456 KB
最終ジャッジ日時 2024-10-18 22:55:27
合計ジャッジ時間 6,391 ms
ジャッジサーバーID
(参考情報)
judge8 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

def count_set_bits_fast(N, x):
    # 2^(x+1)の周期でビットが変化する
    cycle_length = 1 << (x + 1)
    
    # フルサイクルの数
    full_cycles = N // cycle_length
    count_in_full_cycles = full_cycles * (1 << x)
    
    # 残りの部分
    remaining = N % cycle_length
    count_in_remaining = max(0, remaining - (1 << x) + 1)
    
    return count_in_full_cycles + count_in_remaining

T = int(input())
for _ in range(T):
    N = int(input())
    ans = 0
    for i in range(60):
        result = count_set_bits_fast(N, i)
        ans += result
    print(ans)
0