P1582 倒水(贪心 + lowbbit)

https://www.luogu.com.cn/problem/P1582

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,k;
//取出1的个数
int check(int x){
    int c = 0;
    for(;x;x -= x & -x){
        c++;
    }
    return c;
}
int ans;
signed main(){
    ios::sync_with_stdio(0);
    cin >> n >> k;
    while(check(n) > k){
        ans += n & -n;
        n += n & -n;
    }
    cout << ans;
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/xcfxcf/p/12371250.html