A positive integer xx is called a power of two if it can be represented as x=2y, where y is a non-negative integer. So, the powers of two are 1,2,4,8,16,…
You are given two positive integers nn and k. Your task is to represent nn as the sumof exactly k powers of two.
Input
The only line of the input contains two integers nn and k (1≤n≤109, 1≤k≤2?105).
Output
If it is impossible to represent nn as the sum of k powers of two, print NO.
Otherwise, print YES, and then print kk positive integers b1,b2,…,bk such that each of bibi is a power of two, and ∑i=1kbi=n. If there are multiple answers, you may print any of them.
Examples
Input
9 4
output
Copy
YES
1 2 2 4
input
Copy
8 1
output
Copy
YES
8
input
Copy
5 1
output
Copy
NO
input
Copy
3 7
output
Copy
NO
判断一个数n是否可以由k个2的p次幂来组成,p为自然数,p任取。
第一大类情况,判断有没有组成可能
第二大类情况,如果有组成的可能
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
int n,k;
cin>>n>>k;
int cnt2=0,p=0,a[1000];
int copy=n;
while(copy>0)
{
if(copy&1)
cnt2++;
a[p++]=copy&1;
copy=copy>>1;
}
if(k>n||cnt2>k)
{
cout<<"NO";
}
else
{
cout<<"YES"<<endl;
if(cnt2==k)
{
}
else
{
while(cnt2!=k)
{
for(int i=p-1;i>=1;i--)
{
if(a[i]>0)
{
a[i]-=1;
a[i-1]=a[i-1]+2;
cnt2++;
if(cnt2==k)
break;
}
}
}
}
for(int i=0;i<p;i++)
{
for(int j=0;j<a[i];j++)
{
cout<<int(pow(2,i))<<" ";
}
}
}
return 0;
}
注意:pow函数返回的是浮点数,超过一定位数后,会用科学计数法表示
原文:https://www.cnblogs.com/BeautifulWater/p/14882133.html