1. 程式人生 > >[CF]Codeforces Round #529 (Div. 3) [CF]Codeforces Round #529 (Div. 3)

[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 (

n≤109">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 

,bk">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

 

相關推薦

[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 rep

Educational Codeforces Round 51 (Rated for Div. 2)(CF 2018.09.22)

#include<iostream> #include<cstdio> using namespace std; long long l,r; int main() { s

A. Little C Loves 3 I Codeforces Round #511 (Div. 2) 【數學】

pos == force line for 最大 header 應該 XML 題目: Little C loves number ?3? very much. He loves all things about it. Now he has a positive

Codeforces Round #450 (Div. 2)+劉汝佳紫書3-8 (C語言整數模擬除法)

基本模板: #include <iostream> #include <cstdio> using namespace std; int main() { int a

Codeforces Round #345 (Div. 1) & CodeForces 650A Watchmen

A. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input

【打CF,學演算法——一星級】CodeForces 617D Polyline(水題)

There are three points marked on the coordinate plane. The goal is to make a simple polyline, without self-intersections and self-touches, such that it pa

【打CF,學演算法——一星級】CodeForces 701A Cards(水題)

題面: A. Cards time limit per test 1 second memory limit per test 256 megabytes input standar

Codeforces 1099 C. Postcard-字串處理(Codeforces Round #530 (Div. 2))

C. Postcard time limit per test 1 second memory limit per te

Codeforces Round #369 (div.2) 即 Codeforces 711

CodeForces Round #369 (div.2) Descreption A 有N排座位,每排有4個,這4個被分成了兩組。給出每個座位是否是空的,問是否有相鄰的座位。 B 給你一個N * N的幻方,其中缺了一個格子,要求你填上。 C 有N個點M種顏

【打CF,學演算法——一星級】CodeForces 227B Effective Approach (水題)

Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array. According to the boys, linear search works as foll

[日常水題 3.17] codeforces 946 A

greedy 一組數,任意分成兩個部分 .求這兩個部的最大差。 #include <bits/stdc++.h> //#define LOCAL_DEFINE using

3-palindrome CodeForces - 805B (思維)

In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick. He is too selfish, so fo

Technocup 2017 - Elimination Round 1 (Unofficially Open for Everyone, Rated for Div. 2) A

turn quotes eba i++ lac from positive 題意 seq Vasily has a number a, which he wants to turn into a number b. For this purpose, he ca

對字符串'//*[@]/div/p/a[1]/c[2]/a[3]/b'從右向左依次刪除指定字符串

pan 元素 mpi div spa logs compile [1] rip import re s=‘//*[@]‘ a=s+‘/div/p/a[1]/c[2]/a[3]/b‘ c=[1,2] b=a.split(s) #切割 c=b[1].split(‘/‘) #

WEB前端面試題 分別使用2個、3個、5個DIV畫出一個大的紅十字

plain rotate tle copyto XML order bar back tro [html] view plain copy <!DOCTYPE html> <!--兩個DIV--> <html>

著名的3像素Bug(div+img,多出幾像素)

AR com www ott baidu pla .... play tps <div><img src="...."></div> 給img的css設置display: block;/*用來去除div下邊莫名多出來的3px的像pa

【打CF,學演算法——三星級】CF Gym 100548K Last Denfence

題面:  Last Defence Description     Given two integers A and B. Sequence S is defined as follow:  

八.200多萬元得到的創業教訓--從3款產品學到的3

ans 產品 class track 實現 理解 family 開發人員 一個人 摘要:先後開發了3款移動互聯網產品,有兩款產品是從創意階段就參與進去,從中學會了最重要的3點:高速叠代,不斷試錯。了解用戶的需求。 健生幹貨分享:第8篇 第一款社交app:移客ekeo

access數據庫編號轉換成統一3位數長度方法,不足3位前面補零

mat nbsp bsp 不足 函數返回 如果 sel 不足補0 空值處理 select C_CUN+Format(Val(NZ(C_LB)),"000") from LBM 1) 空值處理函數: Nz(表達式)空值處理函數Nz是專門用來處理表達式空值的,如果該表達式是值

4.標準體重: 男士體重=身高-100±3 女士體重=身高-110±3 輸入性別、身高、體重,查看體重是否標準

script amp 體重 == b- pro else if else scrip <script> a=prompt("請輸入性別“,”") b=prompt("請輸入身高“,”") c=prompt("請輸入體重”,“") x="女" y="男" d=b-