The meaning of problems
for
, you have a number of
Please number with a minimum number of composition
answer
First, we can not simply judge this number more than 30,000
first
, so
does not exceed
, when
large,
will remain no more than
, when the
is small, a small portion may be replaced by the larger part (
times)
so far is no more than
We enumerate the number, resulting in
We binary decomposition, get a few numbers, but the number may not equal the enumeration of us.
How to do it
first. One
certainly equal
months
, and each of us put a
down adjustment will be more of a two, a minus removed, one by one every time is added up, that is to say: If you start fewer bits, but the ratio of the total number of enumeration large, is that you can achieve.
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define sf(x) scanf("%d",&x)
using namespace std;
typedef long long ll;
const int maxn = 300000;
const ll mod = 1e9+7;
int n,p;
int num[maxn+100],a[maxn+100];
map<ll,int>mp;
int main(){
cin>>n>>p;
FOR(i,1,maxn){
int m=n-i*p,tmp=m;
if(m<0)break;
int num=0;
while(m){
if(m%2)num++;
m/=2;
}
if(num<=i&&i<=tmp){
cout<<i<<endl;
return 0;
}
}
puts("-1");
}