http://codeforces.com/contest/1247/problem/C
当时的思路贪心枚举就好 不用二分
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
ll p,n;
bool check(ll x,ll i){
if(x<=0) //负的无法用2的指数幂表示
return false;
ll cnt1=0,cnt2=x;
while(x){
if(x&1)
cnt1++;
x>>=1;
}
if(i>=cnt1 && i<=cnt2)
return true;
else
return false;
}
int main(){
cin>>n>>p;
for(int i=1;i<=32;i++){
if(check(n-i*p,i))
{
cout<<i<<endl;
return 0;
}
}
cout<<-1<<endl;
return 0;
}