Salvens

I

Aug 11th, 2023
905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <array>
  4. #include <vector>
  5. #include <numeric>
  6. #include <random>
  7. #include <chrono>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. //#define int long long
  13. #pragma comment(linker,"/STACK:1000000000,1000000000")
  14.  
  15. const long long INF = 1e9 + 7;
  16. const int MAXN = 1e6 + 1000;
  17. const int N = 1e5 + 10;
  18.  
  19. const int M1 = 1e9 + 123;
  20. const int M2 = 1e9 + 321;
  21. int P1 = 22811;
  22. int P2 = 22699;
  23.  
  24. array<int, MAXN> power1, power2;
  25.  
  26. inline void init_pow() {
  27.     power1[0] = 1;
  28.     power2[0] = 1;
  29.     for (int i = 1; i < MAXN; ++i) {
  30.         power1[i] = (1ll * power1[i - 1] * P1) % M1;
  31.         power2[i] = (1ll * power2[i - 1] * P2) % M2;
  32.     }
  33. }
  34.  
  35. inline void build_hash(vector<int>& s, vector<pair<int, int>>& h) {
  36.     int n = s.size();
  37.     h.resize(n + 1);
  38.     h[0].first = 0;
  39.     h[0].second = 0;
  40.  
  41.     for (int i = 0; i < n; ++i) {
  42.         h[i + 1].first = (h[i].first + 1ll * s[i] * power1[i]) % M1;
  43.         h[i + 1].second = (h[i].second + 1ll * s[i] * power2[i]) % M2;
  44.     }
  45. }
  46.  
  47. inline bool is_equal(vector<pair<int, int>>& h_l, vector<pair<int, int>>& h_r, int start1, int start2, int len) {
  48.     pair<int, int> d_l, d_r;
  49.     d_l.first = ((h_l[start1 + len].first - h_l[start1].first + M1) % M1 * 1ll * power1[start2]) % M1;
  50.     d_l.second = ((h_l[start1 + len].second - h_l[start1].second + M2) % M2 * 1ll * power2[start2]) % M2;
  51.  
  52.     d_r.first = ((h_r[start2 + len].first - h_r[start2].first + M1) % M1 * 1ll * power1[start1]) % M1;
  53.     d_r.second = ((h_r[start2 + len].second - h_r[start2].second + M2) % M2 * 1ll * power2[start1]) % M2;
  54.  
  55.     return d_l == d_r;
  56. }
  57.  
  58. inline int random_key(const int before, const int after) {
  59.     auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
  60.     std::mt19937 mt_rand(seed);
  61.     int base = std::uniform_int_distribution<int>(before + 1, after)(mt_rand);
  62.     return base;
  63. }
  64.  
  65. signed main() {
  66.     ios_base::sync_with_stdio(false);
  67.     cin.tie(nullptr);
  68.     cout.tie(nullptr);
  69.  
  70.     P1 = random_key(256, M1);
  71.     P2 = random_key(256, M2);
  72.  
  73.     int n, m;
  74.     cin >> n >> m;
  75.     vector<int> a(n), b(n);
  76.     for (int i = 0; i < n; ++i) {
  77.         cin >> a[i];
  78.         b[i] = a[i];
  79.     }
  80.     reverse(b.begin(), b.end());
  81.  
  82.  
  83.     vector<pair<int, int>> h_a, h_b;
  84.     init_pow();
  85.     build_hash(a, h_a);
  86.     build_hash(b, h_b);
  87.  
  88.  
  89.     for (int i = n / 2 - 1; i < n; ++i) {
  90.         int len = n - i - 1;
  91.         if (is_equal(h_b, h_a, n - i - 1, i + 1, len)) {
  92.             cout << i + 1 << ' ';
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment