Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer ? (which may be positive, negative, or zero). To combine their tastes, they invented ?-binary numbers of the form 2?+?, where ? is a non-negative integer.
For example, some −9-binary (“minus nine” binary) numbers are: −8 (minus eight), 7 and 1015 (−8=20−9, 7=24−9, 1015=210−9).
The boys now use ?-binary numbers to represent everything. They now face a problem: given a positive integer ?, what’s the smallest number of ?-binary numbers (not necessarily distinct) they need to represent ? as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if ?=0 we can represent 7 as 20+21+22.
And if ?=−9 we can represent 7 as one number (24−9).
Note that negative ?-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers ? and ? (1≤?≤109, −1000≤?≤1000).
Output
If it is impossible to represent ? as the sum of any number of ?-binary numbers, print a single integer −1. Otherwise, print the smallest possible number of summands.
Examples
inputCopy
24 0
outputCopy
2
inputCopy
24 1
outputCopy
3
inputCopy
24 -1
outputCopy
4
inputCopy
4 -7
outputCopy
2
inputCopy
1 1
outputCopy
-1
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24=(24+0)+(23+0).
In the second sample case, we can represent 24=(24+1)+(22+1)+(20+1).
In the third sample case, we can represent 24=(24−1)+(22−1)+(22−1)+(22−1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4=(24−7)+(21−7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible.
思路:昨晚比赛的时候有想到考虑到计算n和p之间的关系和转化成二进制后有多少个1,但是题意没读懂,一直wa。
题意是给n和p,能转换成最少的不同组合 (二进制-p),详细见代码注释。
#include
#define ll long long
#define R register int
#define inf 0x3f3f3f3f
#define mod 1000000007;
using namespace std;
inline ll read(){
ll s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put1(){ puts("Yes") ;}
void put2(){ puts("No") ;}
const int manx=2e5+5;;
int main()
{
ll n,k;
n=read(),k=read();
n-=k; //必须先减去一个形成第一个n-k
for(int i=1;i<=n;i++,n-=k) //遍历循环
{
int t=__builtin_popcountll(n);
//查找当前这个n (这个时候n的含义已经不是原本的n,而是n-i*p)的二进制有多少个1
if(t<=i) //如果1的个数比i还大那肯定无法满足条件,必须继续遍历
{ //当1的个数比i小即表示不同的(2^x-p)的最小值被找到了
cout<<i<<endl;
return 0;
}
}
puts("-1");
return 0;
}