Advertisement
Kevin_Zhang

Untitled

Sep 24th, 2022
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4. #define pb emplace_back
  5. #define AI(i) begin(i), end(i)
  6. template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
  7. template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
  8. #ifdef KEV
  9. #define DE(args...) kout("[ " + string(#args) + " ] = ", args)
  10. void kout() { cerr << endl; }
  11. template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
  12. template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
  13. #else
  14. #define DE(...) 0
  15. #define debug(...) 0
  16. #endif
  17. // My bug list :
  18. // integer overflow
  19. // 0based, 1based forgotten
  20. // index out of bound
  21. // n, m, i, j typo
  22. // some cases are missing
  23.  
  24. const int MAX_N = 300010;
  25.  
  26. struct LFSR {
  27.     int state, tap, len;
  28.     int getbit() {
  29.         int ret = state & 1;
  30.         int f = __builtin_popcount(state & tap) & 1;
  31.         state = (state >> 1) | (f << (len-1));
  32.         return ret;
  33.     }
  34.     LFSR(vector<int> taps, int state, int len) :
  35.         state(state), len(len) {
  36.             for (int i : taps)
  37.                 tap |= 1 << i;
  38.         }
  39. };
  40. ostream& operator << (ostream& out, LFSR b) {
  41.     for (int i = 0;i < b.len;++i) out << (b.state>>i&1);
  42.     return out;
  43. }
  44.  
  45. const vector<int> output = {1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 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, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1};
  46. const int flag_len = output.size() - 200;
  47.  
  48. int most_same;
  49. int check(LFSR a) {
  50.     int same = 0;
  51.     for (int i = 0;i < flag_len;++i)
  52.         a.getbit();
  53.     for (int i = flag_len;i < output.size();++i)
  54.         same += a.getbit() == output[i];
  55.     chmax(most_same, same);
  56.     return same;
  57.     return same >= 150;
  58. }
  59. LFSR find_first(vector<int> taps, int len) {
  60.     DE(len);
  61.     int mb = 0;
  62.     int sm = 0;
  63.     LFSR ret = LFSR(taps, 1, 1);
  64.     for (int s = 0;s < 1<<len;++s) {
  65.         if (chmax(sm, check(LFSR(taps, s, len)))) {
  66.             ret = LFSR(taps, s, len);
  67.             DE(sm);
  68.         }
  69.         //if (check(LFSR(taps, s, len)))
  70.             //return LFSR(taps, s, len);
  71.     }
  72.     return ret;
  73.     //__builtin_unreachable();
  74. }
  75.  
  76. struct trilLFSR {
  77.     LFSR a, b, c;
  78.     trilLFSR(LFSR a, LFSR b, LFSR c) :
  79.         a(a), b(b), c(c) {}
  80.     int getbit() {
  81.         int x = a.getbit(), y = b.getbit(), z = c.getbit();
  82.         return x ? y : z;
  83.     }
  84. };
  85.  
  86. bool check1(trilLFSR g) {
  87.     for (int i = 0;i < flag_len;++i) {
  88.         g.getbit();
  89.     }
  90.     for (int i = flag_len;i < output.size();++i) {
  91.         if (g.getbit() != output[i]) return false;
  92.     }
  93.     return true;
  94. }
  95.  
  96. void getflag(trilLFSR g) {
  97.     char c = 0;
  98.     for (int i = 0;i < flag_len;++i) {
  99.         if (i % 8 == 0) {
  100.             cout << c;
  101.             c = 0;
  102.         }
  103.         c = (c << 1) | (g.getbit() ^ output[i]);
  104.     }
  105. }
  106.  
  107. int32_t main() {
  108.     ios_base::sync_with_stdio(0), cin.tie(0);
  109.  
  110.     auto lfsr2 = find_first({0, 5, 7, 22}, 23);
  111.     DE(most_same);
  112.     return 0;
  113.     auto lfsr3 = find_first({0, 17, 19, 24}, 25);
  114.  
  115.     DE(lfsr2, lfsr3);
  116.  
  117.     return 0;
  118.     const vector<int> lfsr1_p = {0, 13, 16, 26};
  119.     const int lfsr1_sz = 27;
  120.     for (int s = 0;s < 1<<27;++s) {
  121.         auto lfsr1 = LFSR(lfsr1_p, s, lfsr1_sz);
  122.         if (check1(trilLFSR(lfsr1, lfsr2, lfsr3)))
  123.             getflag(trilLFSR(lfsr1, lfsr2, lfsr3));
  124.     }
  125.  
  126.  
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement