From c6889ffebd469999c3ac0b2b26801c354c1c9cdf Mon Sep 17 00:00:00 2001 From: LogSingleDog <1034028483@qq.com> Date: Mon, 22 Jul 2024 17:27:53 +0800 Subject: [PATCH] update hash --- "1-\345\255\227\347\254\246\344\270\262.md" | 43 +++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git "a/1-\345\255\227\347\254\246\344\270\262.md" "b/1-\345\255\227\347\254\246\344\270\262.md" index cb2d61e..c90ffa1 100644 --- "a/1-\345\255\227\347\254\246\344\270\262.md" +++ "b/1-\345\255\227\347\254\246\344\270\262.md" @@ -132,6 +132,49 @@ ull get(int l, int r) return (Hash[r] - (Hash[l - 1] * base[r - l + 1]) % mod + mod) % mod; } ``` +### Our Hash +```cpp + const int N = 1050000; + array mod = {1572869,6291469}; + array bas = {131,137}; + array base[N]; + string a; + int lena; + array hasha[N]; +void init(){ + ll tmp1; + base[0] = {1,1}; + for(int i=1;i<=N*2-2;i++){ + for(int j=0;j<2;j++){ + tmp1 = 1ll*base[i-1][j]*bas[j];tmp1%=mod[j]; + base[i][j] = tmp1%mod[j]; + } + } +} +void geta(){ + hasha[0] = {0,0}; + ll tmp1; + for(int i=1;i<=2*lena;i++){ + for(int j=0;j<2;j++){ + tmp1 = 1ll*hasha[i-1][j]*base[1][j]; + tmp1%=mod[j]; + hasha[i][j] = tmp1+a[i] - 'A'; + hasha[i][j]%=mod[j]; + } + } +} +array cala(int l){ + array res={0,0}; + int r = l+lena-1; + ll tmp1; + for(int i=0;i<2;i++){ + tmp1 = 1ll*hasha[l-1][i]*base[r-l+1][i];tmp1%=mod[i]; + res[i] = (hasha[r][i]-(tmp1)%mod[i]+mod[i])%mod[i]; + res[i]%=mod[i]; + } + return res; +} +```