intrk_hash(const std::string s, int start, int base, int L, int mod) { int hash = 0; for (int i = 0; i < L; i++) { hash = (hash * base) % mod; hash = (hash + s[start + i]) % mod; } return hash; }
intpowmod(int a, int n, int m){ if (n == 1) return a; if (n % 2 == 0) returnpowmod((longlong)a * a % m, n / 2, m) % m; return ((powmod((longlong)a * a % m, n / 2, m) % m) * ((longlong)a % m)) % m; }
intrk_cont_hash(const std::string s, int start, int last_h, int base, int L, int mod) { int hash = last_h - s[start - 1] * powmod(base, L - 1, mod); hash = (hash * base) % mod; hash = (hash + s[start + L - 1]) % mod; return hash; }