Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- typedef long long ll;
- const int mod = 1000000007;
- const ll param = 37;
- vector<ll> CalcPrefHashes(const string& s) {
- vector<ll> pref_hashes(s.length() + 1);
- ll hash = 0;
- for (int i = 0; i < s.length(); ++i) {
- pref_hashes[i + 1] = hash = (hash * param + s[i]) % mod;
- }
- return pref_hashes;
- }
- vector<ll> CalcPowers(ll p, int n) {
- if (n == 0) return {};
- vector<ll> powers(n);
- powers[0] = 1ll;
- for (int i = 1; i < n; ++i) powers[i] = powers[i - 1] * p % mod;
- return powers;
- }
- int main() {
- string s; cin >> s;
- vector<ll> pref_hashes = CalcPrefHashes(s);
- vector<ll> powers = CalcPowers(param, s.length());
- int l, r; cin >> l >> r;
- // Хеш от l включительно до r невключительно.
- ll hash = (pref_hashes[r] - (pref_hashes[l - 1] * powers[r - l]) % mod + mod) % mod;
- cout << hash << endl;
- // for (ll x : pref_hashes) cout << x << " ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement