Advertisement
boredpanda2020

Untitled

Aug 13th, 2021
1,212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. #define endl "\n"
  5. #define MOD 1000000007
  6. #define mod 998244353
  7. #define ar array
  8. #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  9.  
  10. void test_case() {
  11.     int n; cin >> n;
  12.     vector<int> a(n);
  13.     bool zero = true;
  14.     for (int i = 0; i < n; i++) {
  15.         cin >> a[i];
  16.         if (a[i] == 1) zero = false;
  17.     }
  18.     if (zero) {
  19.         cout << 0 << "\n";
  20.         return;
  21.     }
  22.  
  23.     // a = 0010 --> 0100
  24.     reverse(a.begin(), a.end());
  25.     vector<int> res(105);
  26.     for (int i = 0; i < n; i++) {
  27.         if (a[i] == 0) {
  28.             // convert number to 2*n
  29.             int carry = 0;
  30.             for (int j = 0; j < 104; j++) {
  31.                 int num_here = 2*res[j] + carry;
  32.                 carry = num_here/6;
  33.                 res[j] = num_here%6;
  34.             }
  35.             assert(carry == 0);
  36.         }
  37.         else {
  38.             // convert number to 2*n + 1
  39.             int carry = 0;
  40.             for (int j = 0; j < 104; j++) {
  41.                 int num_here = 2*res[j] + carry;
  42.                 carry = num_here/6;
  43.                 res[j] = num_here%6;
  44.             }
  45.             carry = 1;
  46.             for (int j = 0; j < 104; j++) {
  47.                 int num_here = res[j] + carry;
  48.                 carry = num_here/6;
  49.                 res[j] = num_here%6;
  50.             }
  51.             assert(carry == 0);
  52.         }
  53.     }
  54.     assert(res.back() == 0);
  55.     while (res.back() == 0) res.pop_back();
  56.     for (int i = 0; i < (int) res.size(); i++) {
  57.         if (i > 0) cout << " ";
  58.         cout << res[i];
  59.     }
  60.     cout << endl;
  61. }
  62.  
  63. int32_t main() {
  64.     fast_io;
  65.  
  66.     int t; cin >> t;
  67.     while (t--) {
  68.         test_case();
  69.     }
  70.  
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement