CodeForces - 1225C p-binary(思维)
题目链接: http://codeforces.com/problemset/problem/1225/C
思路
把所有的p移回左边得到一个数, 此时它应该有多个\({2^k}\) 组成 然后我们就可以数出这个数的二进制及有多少个1 判断它是不是符合题意
Code
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define popcount __builtin_popcount
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int n, p;
cin >> n >> p;
for (int i = 1; i < 100000000; ++i) {
int num = n - p * i;
if(num < i) break;
if (i >= popcount(num)) {
cout << i << endl;
return 0;
}
}
cout << "-1" << endl;
return 0;
}