Beingamanforever

471D CF

Mar 24th, 2025
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // #include <atcoder/all>
  3. using namespace std;
  4. #define int long long
  5. #define all(x) (x).begin(), (x).end()
  6. typedef vector<int> vi;
  7. typedef vector<vi> vvi;
  8. typedef vector<pair<int, int>> vpi;
  9. typedef pair<int, int> pi;
  10. #define f first
  11. #define s second
  12. #define pb push_back
  13. #define endl "\n"
  14. #define yes cout << "YES" << endl
  15. #define no cout << "NO" << endl
  16. int dx[] = {-1, 0, 1, 0};
  17. int dy[] = {0, 1, 0, -1};
  18. const int mod1 = 1e9 + 7, mod2 = 998244353, INF = 2e18, N = 2e5 + 5, L = 19;
  19. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  20. // -----------------------------------------------------------------------------
  21. struct Hash
  22. {
  23.     const int MOD = 1e9 + 7;
  24.     const int base1 = 5953; // prime1
  25.     const int base2 = 9733; // prime2
  26.     vpi hashes, pow;
  27.     vi diffs;
  28.     Hash(vi &s)
  29.     {
  30.         int n = s.size();
  31.         hashes.resize(n + 1, {0, 0});
  32.         pow.resize(n + 1, {1, 1});
  33.         for (int i = 1; i < n; i++)
  34.         {
  35.             diffs.pb({s[i] - s[i - 1]});
  36.         }
  37.         n = diffs.size();
  38.         for (int i = 0; i < n; i++)
  39.         {
  40.             int value = (diffs[i] % MOD) + MOD;
  41.             hashes[i + 1].first = (hashes[i].first * base1 + value) % MOD;
  42.             hashes[i + 1].second = (hashes[i].second * base2 + value) % MOD;
  43.             pow[i + 1].first = (pow[i].first * base1) % MOD;
  44.             pow[i + 1].second = (pow[i].second * base2) % MOD;
  45.         }
  46.     }
  47.     pair<int, int> gethash(int l, int r)
  48.     {
  49.         l++, r++;
  50.         int hash1 = (hashes[r].first - (1LL * hashes[l - 1].first * pow[r - l + 1].first) % MOD + MOD) % MOD;
  51.         int hash2 = (hashes[r].second - (1LL * hashes[l - 1].second * pow[r - l + 1].second) % MOD + MOD) % MOD;
  52.         return {hash1, hash2};
  53.     }
  54.     // Hash ht(t) // auto it = ht.gethash(l,r)
  55. };
  56. void solve()
  57. {
  58.     int n, m;
  59.     cin >> n >> m;
  60.     vi a(n), b(m);
  61.     for (int i = 0; i < n; i++)
  62.     {
  63.         cin >> a[i];
  64.     }
  65.     for (int i = 0; i < m; i++)
  66.     {
  67.         cin >> b[i];
  68.     }
  69.     if (m == 1)
  70.     {
  71.         cout << n << endl;
  72.         return;
  73.     }
  74.     if (m > n)
  75.     {
  76.         cout << 0 << endl;
  77.         return;
  78.     }
  79.     Hash a1(a), b1(b);
  80.     // hash the differences
  81.     auto check = b1.gethash(0, m - 2);
  82.     int ans = 0;
  83.     for (int i = 0; (i + m - 2) <= n; i++)
  84.     {
  85.         if ((a1.gethash(i, i + m - 2)) == check)
  86.         {
  87.             ans++;
  88.         }
  89.     }
  90.     cout << ans << endl;
  91.     return;
  92. }
  93. signed main()
  94. {
  95.     // __START__;
  96.     ios_base::sync_with_stdio(false);
  97.     cin.tie(NULL);
  98.     cout.tie(NULL);
  99.     int t = 1;
  100.     // cin >> t;
  101.     while (t--)
  102.     {
  103.         solve();
  104.     }
  105.     // __END__;
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment