fork download
  1. /*صل علي النبي*/
  2. #include <bits/stdc++.h>
  3. #include <unordered_set>
  4. #include <unordered_map>
  5. //#include <ext/pb_ds/assoc_container.hpp>
  6. //#include <ext/pb_ds/tree_policy.hpp>
  7. //using namespace __gnu_pbds;
  8. using namespace std;
  9. #define ll long long
  10. #define all(v) ((v).begin()), ((v).end())
  11. #define sz(v) ((int)((v).size()))
  12. #define Please_Not_Focus_To_My_Code ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  13. //template<class T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
  14. #define testCase int t;cin>>t;while(t--)
  15. const int mod = 1e9 + 7;
  16.  
  17. //ceil -> (n + (d - 1))/d
  18. //round+ -> (n + (d/2))/d
  19. //round- -> (n - (d/2))/d
  20. //getline(cin, s): for strings with spaces
  21. // compare float nums abs(a-b) < 1e-9
  22.  
  23. void elwkel(){
  24. ll n, k , sum{};
  25. cin >> n >> k;
  26. int v[32]{};
  27. for (int i = 0; i < 31; i++) if(n & (1 << i))
  28. v[i]++ , sum++;
  29.  
  30. if(sum == k){ // First Case when number of one`s in binary number == k
  31. cout << "YES\n";
  32. for (int i = 0; i < 32; i++){
  33. for (int j = 0; j < v[i]; j++) if(v[i])
  34. cout << (1 << i) << ' ';
  35. }
  36. }
  37.  
  38. else if(sum > k) // Second Case when when number of one`s in binary number > k -> IMPOSSIBLE
  39. cout << "NO\n";
  40.  
  41. else{ // Last Case when when number of one`s in binary number > k -> lets shift greatest one
  42. // in binary to prev bit (8 = 2 * 4 :) )
  43. int cnt {};
  44. for (int i = 31; i > 0; i--){
  45. if(v[i]){
  46. while(v[i] > 0){
  47. v[i]-- , v[i - 1]+=2 ,cnt++;
  48.  
  49. if(cnt + sum == k) break;
  50. }
  51. }
  52.  
  53. if(cnt + sum == k) break;
  54. }
  55.  
  56. if (cnt + sum != k)
  57. cout << "NO\n";
  58.  
  59. else{
  60. cout << "YES\n";
  61. for (int i = 0; i < 32; i++){
  62. if (v[i]){
  63. while (v[i]>0){
  64. v[i]--;
  65. cout << (1 << i) << ' ';
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72.  
  73.  
  74. signed main(){
  75. /*<<<<<*/Please_Not_Focus_To_My_Code/*>>>>>*/
  76. // testCase
  77. {
  78. /*/\/\/\/\/\/\/\/\/\/\/\/*/
  79. /*/////*/elwkel();/*\\\\\*/
  80. /*/\/\/\/\/\/\/\/\/\/\/\/*/
  81. }
  82. }
Success #stdin #stdout 0.01s 5300KB
stdin
9 4
stdout
YES
1 2 2 4