Advertisement
Shuva_Dev

Power of Two - Codeforces - 1095C

Dec 30th, 2022
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define endl "\n"
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.  
  8.     int n, k;
  9.     cin >> n >> k;
  10.     vector<int> v;
  11.  
  12.     if(n < k) cout << "NO";
  13.     else {
  14.         int x = log2(n) + 1, j=0;
  15.         // J = Number of 1 in binary represention of n
  16.         if(n & 1) j=1;
  17.         for(int i = 1; i<x; i++) {
  18.             if(n & (1 << i)) {
  19.                 j++;
  20.                 v.push_back(1 << i);
  21.             }
  22.         }
  23.        
  24.         if(k < j) cout << "NO";
  25.         else {
  26.             cout << "YES\n";
  27.             for(; j<k; j++) {
  28.                 int num = *(v.end() - 1);
  29.                 if( num / 2 == 1 ) {
  30.                     v.pop_back();
  31.                 } else {
  32.                     v.pop_back();
  33.                     v.push_back(num/2);
  34.                     v.push_back(num/2);
  35.                 }
  36.             }
  37.             int c = 0;
  38.             for(auto u:v) {
  39.                 printf("%d ",u);
  40.                 c++;
  41.             }
  42.             for(int i=1; i<=(k-c); i++) printf("1 ");
  43.         }
  44.     }
  45.    
  46.    
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement