Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //********************String Hashing****************///
- #include <bits/stdc++.h>
- #define maxx 100005
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- ll BigMod(ll a,ll r,ll Mod)
- {
- ll ret=1;
- while(r) {
- if(r&1) {
- ret=ret*a;
- ret=ret%Mod;
- }
- a=a*a;
- a=a%Mod;
- r=r/2;
- }
- return ret;
- }
- //InverseMOD
- ll InverseMod(ll a,ll Mod)
- {
- return BigMod(a,Mod-2,Mod);
- }
- ll base[maxx];
- ll mod=293114467;
- void basegenerate(ll bz)
- {
- base[0]=1;
- for(ll i=1; i<maxx; i++)
- {
- base[i]=(base[i-1]*bz)%mod;
- }
- }
- ll hashing(string s, ll (&encode)[maxx])
- {
- ll l=s.size();
- encode[0] = (s[0]*base[0])%mod;
- for(ll i=1; i<l; i++)
- {
- encode[i]=(encode[i-1]+((s[i]*base[i])%mod))%mod;
- }
- }
- ll hashValue(ll l, ll r, ll (&encode)[maxx])
- {
- if(l==0) return encode[r];
- return ((((encode[r]-encode[l-1])%mod)*InverseMod(base[l],mod))%mod+mod)%mod;
- }
- int main()
- {
- //freopen("testcases.txt","r",stdin);
- ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- basegenerate(1000);
- string s;
- while(cin>>s)
- {
- ll arr[maxx];
- memset(arr, 0, sizeof(arr));
- hashing(s, arr);
- ll x,y; cin>>x>>y;
- cout << hashValue(x, y, arr) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement