Advertisement
smatskevich

Lesson16

Mar 18th, 2023 (edited)
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6. typedef long long ll;
  7. const int mod = 1000000007;
  8. const ll param = 37;
  9.  
  10. vector<ll> CalcPrefHashes(const string& s) {
  11.   vector<ll> pref_hashes(s.length() + 1);
  12.   ll hash = 0;
  13.   for (int i = 0; i < s.length(); ++i) {
  14.     pref_hashes[i + 1] = hash = (hash * param + s[i]) % mod;
  15.   }
  16.   return pref_hashes;
  17. }
  18.  
  19. vector<ll> CalcPowers(ll p, int n) {
  20.   if (n == 0) return {};
  21.   vector<ll> powers(n);
  22.   powers[0] = 1ll;
  23.   for (int i = 1; i < n; ++i) powers[i] = powers[i - 1] * p % mod;
  24.   return powers;
  25. }
  26. int main() {
  27.   string s; cin >> s;
  28.   vector<ll> pref_hashes = CalcPrefHashes(s);
  29.   vector<ll> powers = CalcPowers(param, s.length());
  30.  
  31.   int l, r; cin >> l >> r;
  32.   // Хеш от l включительно до r невключительно.
  33.   ll hash = (pref_hashes[r] - (pref_hashes[l - 1] * powers[r - l]) % mod + mod) % mod;
  34.   cout << hash << endl;
  35.  
  36. //  for (ll x : pref_hashes) cout << x << " ";
  37.   return 0;
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement