[CF]Codeforces Round #529 (Div. 3) [CF]Codeforces Round #529 (Div. 3)

[CF]Codeforces Round #529 (Div. 3)

C. Powers Of Two

Description

A positive integer xx is called a power of two if it can be represented as x=2yx=2y, where yy is a non-negative integer. So, the powers of two are 1,2,4,8,16,1,2,4,8,16,….

You are given two positive integers nn and kk. Your task is to represent nn as the sum of exactlykpowers of two.

Input

The only line of the input contains two integers nn and kk (1n1091≤n≤109, 1k21051≤k≤2⋅105).

output

If it is impossible to represent nn as the sum of kk powers of two, print NO.

Otherwise, print YES, and then print kk positive integers b1,b2,,bkb1,b2,…,bk such that each of bibi is a power of two, and i=1kbi=n∑i=1kbi=n. If there are multiple answers, you may print any of them.

Examples

Input

9 4

Output

YES
1 2 2 4

Input

5 1

Output

NO

正确解法:

题目的意思是说,从2的次幂中找出k个,使他们相加等于n。

我刚开始想的暴搜,从1 1 1 2 2  4 8 等等一个一个找。

我们只要贪心,从大到小就好,找到一个方案就可以。

于是只要满足 n-  2的次幂  >=k-1 

如果找到有k个,就可以了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string>
 4 #include<cstring>
 5 #include<map>
 6 #include<set>
 7 #include<algorithm>
 8 #include<cmath>
 9 #include<cstdlib>
10 using namespace std;
11 int a[40],f[200100],cnt=0;
12 int n, k; 
13 int main()
14 {
15     a[0] = 1;
16     for (int i = 1; i <= 30; i++)
17         a[i] = 2 * a[i - 1];
18     scanf("%d %d",&n,&k);
19     for (int i = 30; i >= 0; i--)
20     {
21         while (n - a[i] >= k - 1 &&n&&k)
22         {
23             n = n - a[i];
24             f[++cnt] = a[i];
25             k--;
26         }
27     }
28     if (n==0)
29     {
30         cout << "YES" << endl;
31         for (int i = cnt; i>1; i--)
32             printf("%d ",f[i]);
33         printf("%d\n",f[1]);
34     }
35     else cout << "NO" << endl;
36     return 0;
37 }
38     
View Code

猜你喜欢

转载自www.cnblogs.com/Kaike/p/10190876.html