Created
February 24, 2020 03:37
-
-
Save andriybuday/d2009edaa98f993770f49a45d24fc520 to your computer and use it in GitHub Desktop.
RollingHash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int mod = 1000000007; | |
int p = 97; //prime | |
long hash = 0, power = 1; | |
for(char c: str) { | |
long add = ((long)c * power); | |
hash += add; | |
hash %= mod; | |
power = power * p % mod; | |
} | |
// in mod space x/p is the same as x*pinv | |
int pinv = BigInteger.valueOf(p).modInverse(BigInteger.valueOf(mod)).intValue(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment