mrlolthe1st

Untitled

Oct 30th, 2023
1,117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <vector>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <map>
  8. #include <numeric>
  9. #include <deque>
  10. #include <set>
  11. #include <functional>
  12. #include <queue>
  13. #include <fstream>
  14. #include <string>
  15. #include <random>
  16. #include <sstream>
  17. #include <thread>
  18.  
  19. using namespace std;
  20. const int mod = 998244353;
  21. int64_t binpow(int64_t a, int64_t b) {
  22.     int64_t r = 1;
  23.     while (b) {
  24.         if (b & 1) r = (r * a) % mod;
  25.         a = (a * a) % mod;
  26.         b >>= 1;
  27.     }
  28.     return r;
  29. }
  30. unordered_map<int, int> factorize(int n) {
  31.     unordered_map<int, int> res;
  32.     for (int i = 2; i * i <= n; ++i) {
  33.         while (!(n % i)) n /= i, res[i]++;
  34.     }
  35.     if (n != 1) res[n]++;
  36.     return res;
  37. }
  38. const int Y = 17;
  39. string solve1(const string& cs) {
  40.     string s = cs;
  41.     if (s.size() == 1) {
  42.         return s + s + s;
  43.     }
  44.     for (auto& e : s) e -= '0';
  45.     vector<int> ind(s.size());
  46.     iota(ind.begin(), ind.end(), 0);
  47.     vector<int> a1, a2;
  48.     vector<int> add;
  49.     int c = 0;
  50.     for (int i = 1; i <= Y; ++i) {
  51.         int w = 1 << i;
  52.         a1.clear();
  53.         a2.clear();
  54.         for (int j = 0; j < s.size(); ++j) {
  55.             if (j % w >= w / 2) a1.push_back(j);
  56.             else a2.push_back(j);
  57.         }
  58.         c = 0;
  59.         if (a1.size() < a2.size()) swap(a1, a2);
  60.         for (auto& e : a1) c ^= s[e];
  61.         add.push_back(c);
  62.     }
  63.     for (auto& e : add) s.push_back(e);
  64.     for (auto& e : s) e += '0';
  65.     return s;
  66. }
  67.  
  68. string solve2(const string& ss) {
  69.     if (ss.size() == 3) {
  70.         if (ss[0] == ss[1]) return string() + ss[0];
  71.         return string() + ss[2];
  72.     }
  73.     int n = ss.size() - Y;
  74.     string s = ss;
  75.     for (auto& e : s) e -= '0';
  76.  
  77.     bool was = 0;
  78.     int c = 0;
  79.     for (int i = 0; i < n; ++i) c ^= s[i];
  80.     int save = c;
  81.     vector<int> ind(n);
  82.     iota(ind.begin(), ind.end(), 0);
  83.     set<int> ok(ind.begin(), ind.end());
  84.     vector<int> a1, a2;
  85.     int pos = n;
  86.     int cnt_neq = 0;
  87.     auto foo = [&]() {
  88.         for (int i = 1; i <= Y; ++i) {
  89.             int w = 1 << i;
  90.             a1.clear();
  91.             a2.clear();
  92.             for (int j = 0; j < n; ++j) {
  93.                 if (j % w >= w / 2) a1.push_back(j);
  94.                 else a2.push_back(j);
  95.             }
  96.             if (a1.size() < a2.size()) swap(a1, a2);
  97.             c = 0;
  98.             for (auto& e : a1) c ^= s[e];
  99.             if (c == s[pos]) {
  100.                 for (auto& e : a1) ok.erase(e);
  101.             }
  102.             else {
  103.                 cnt_neq = i;
  104.                 for (auto& e : a2) ok.erase(e);
  105.             }
  106.             ++pos;
  107.         }
  108.         };
  109.     foo();
  110.     if (cnt_neq && ok.size()) {
  111.         int ps = *ok.begin();
  112.         s[n + cnt_neq - 1] ^= 1;
  113.         cnt_neq = 0;
  114.         pos = n;
  115.         foo();
  116.         if (cnt_neq) {
  117.             s[ps] ^= 1;
  118.         }
  119.     }
  120.     for (auto& e : s) e += '0';
  121.     return s.substr(0, n);
  122. }
  123. mt19937 gen(12412);
  124. vector<int> ww(20);
  125. void gn(int l, int r, int d = 1) {
  126.     if (l >= r) return;
  127.     ww[d]++;
  128.     gn(l, (l + r) / 2, d + 1);
  129.     gn(1 + (l + r) / 2, r, d + 1);
  130. }
  131. int main(int argc, char* argv[]) {
  132.     /*ios::sync_with_stdio(0);
  133.     cin.tie(0);
  134.     cout.tie(0);*/
  135.     gn(1, 1e5);
  136.     while (1) {
  137.         int l = 1e5;
  138.         string s;
  139.         for (int i = 0; i < l; ++i) s.push_back('0' + (rand() & 1));
  140.         auto r1 = solve1(s);
  141.         auto rr2 = r1;
  142.         rr2[gen() % (rr2.size())] ^= 1;
  143.         auto r2 = solve2(rr2);
  144.         cout << rr2.size() << "\n";
  145.         if (r2 != s) {
  146.             solve2(rr2);
  147.             return -1;
  148.         }
  149.     }
  150.     int t;
  151.     cin >> t;
  152.     if (t == 1) {
  153.         string s; cin >> s;
  154.         cout << solve1(s) << "\n";
  155.     }
  156.     else {
  157.         string s;
  158.         cin >> s;
  159.         cout << solve2(s) << "\n";
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment