Stay Hungry,Stay Foolish!

D - Masked Popcount

D - Masked Popcount

https://atcoder.jp/contests/abc356/tasks/abc356_d

 

思路

对于m的所有为1的bit位置,统计 0 ~ n 中所有对应对bit位置中1的个数,

如何统计呢?

观察如下bit位表:

从右向左第一位 循环节为  0 1

从右向左第一位 循环节为  0 0 1 1

从右向左第一位 循环节为  0 0 0 0 1 1 1 1

 

 

0   ---   0 0 0 0         
1   ---   0 0 0 1
2   ---   0 0 1 0
3   ---   0 0 1 1
4   ---   0 1 0 0
5   ---   0 1 0 1
6   ---   0 1 1 0
7   ---   0 1 1 1
8   ---   1 0 0 0

 

实现上注意:

(1)最后一个非完整循环块中余数中1的统计

(2)循环的规律是按照数序规律 0 对应 第1,  1对应 第2, ...  n对应第n+1

Code

https://atcoder.jp/contests/abc356/submissions/54397329

#include <bits/stdc++.h>
using namespace std;
const long long mod=998244353;
int main(){
    long long n,m;
    cin>>n>>m;
    n++;
    long long ans=0;
    for(int i=0;i<60;i++){
        if(m&(1ll<<i)){
            ans+=(n)/(1ll<<(i+1))%mod*(1ll<<i)%mod;
            ans%=mod;
            if(n%(1ll<<(i+1)))ans+=max(0ll,n%(1ll<<(i+1))-(1ll<<i));
//            cout<<ans;
ans%=mod;
        }
    }
    cout<<ans;
}

 

posted @ 2024-06-09 14:16  lightsong  阅读(5)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel