Josif_tepe

Untitled

Feb 11th, 2026
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <vector>
  4. #include <cmath>
  5. using namespace std;
  6. const int maxn = 1e6;
  7. const int INF = 2e9;
  8. typedef long long ll;
  9.  
  10. int n;
  11.  
  12. string to_binary(int x) {
  13.     string res = "";
  14.    
  15.     while(x > 0) {
  16.         res += (x % 2) + '0';
  17.         x /= 2;
  18.     }
  19.    
  20.     while((int) res.size() < n) {
  21.         res += "0";
  22.     }
  23.    
  24.     reverse(res.begin(), res.end());
  25.     return res;
  26. }
  27. int main() {
  28.     ios_base::sync_with_stdio(false);
  29.     cin >> n;
  30.    
  31.     int power_of_2 = (1 << n);
  32.    
  33.     vector<int> v = {1, 2, 3};
  34.     for(int i = 0; i < power_of_2; i++) {
  35.         string s = to_binary(i);
  36.        
  37.         for(int j = 0; j < (int) s.size(); j++) {
  38.             if(s[j] == '1') {
  39.                 cout << v[j] << " ";
  40.             }
  41.         }
  42.         cout << endl;
  43.     }
  44.     return 0;
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment