跳到正文

题解

题解:P16337 「ALFR Round 10」Bit Problem

luogu_LiuShaRui约 1 分钟
原文
文章信息与版本

文章信息

发布
2026年4月19日
收录
2026年4月19日
更新
2026年11月14日
篇幅
约 1 分钟
题目
P16337

版本

  1. v2当前
  2. v1

题解:P16337 「ALFR Round 10」Bit Problem

思路

注意可能 m=0m=0。

其余情况,每次按位与运算都肯定去掉一个二进制的 11,因为减一个数要么去高位或者去非高位的 11,所以最多去掉 log⁡2n\log_2 n 个 11,直接循环时判断是否删完了即可。因为最多只需要删 log⁡2n\log_2 n 次,所以实际时间复杂度为 O(Tlog⁡n)O(T \log n)。

code

C++
#include<bits/stdc++.h>
using namespace std;
int main(){
    int T;
    cin>>T;
    while(T--){
        int n,m,k;
        cin>>n>>m>>k;
        if(m==0){
            cout<<n<<'\n';
            continue;
        }
        for(int i=1;i<=k;i++){
            if(n<m||n==0){
                n=0;
                break;
            }
            n=n&(n-m);
        }
        cout<<n<<'\n';
    }
    return 0;
}
原文