题意:给两个数 p-binary 数为 p-binary 数,求出拆分得到的最少 p-binary 数,如果无解输出
假设 p-binary 数,那么有如下式子:
很明显,我们可以从小到大枚举
如果
证明:如果
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int n,p,ans=1;
int clac(int x)
{
int sum=0;
while(x)
{
if(x&1)
sum++;
x>>=1;
}
return sum;
}
int main()
{
scanf("%d%d",&n,&p);
while(n-ans*p>=ans)
{
int x=n-ans*p;
int num=clac(x);
if(num<=ans)
{
printf("%d",ans);
return 0;
}
ans++;
}
printf("-1");
return 0;
}