Advertisement
Ankit_132

B

May 26th, 2024
936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5.     int t;
  6.     cin>>t;
  7.  
  8.     while(t--){
  9.         int x;
  10.         cin>>x;
  11.  
  12.         vector<int> v(32);
  13.  
  14.         for(int i=29; i>=0; i--){
  15.             v[i] = (x >= (1<<i));
  16.             x %= (1<<i);
  17.         }
  18.  
  19.         for(int i=0, j; i<32;){
  20.  
  21.             if(!v[i]){
  22.                 i++;
  23.                 continue;
  24.             }
  25.  
  26.             j = i+1;
  27.             while(v[j])
  28.             {
  29.                 v[j] = 0;
  30.                 j++;
  31.             }
  32.  
  33.             if(j > i+1)
  34.             {
  35.                 v[j] = 1;
  36.                 v[i] = -1;
  37.             }
  38.  
  39.             i = j;
  40.         }
  41.  
  42.         cout<<32<<"\n";
  43.         for(auto e: v)
  44.             cout<<e<<" ";
  45.         cout<<"\n";
  46.  
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement