Link to the topic: Problem - 5685 (hdu.edu.cn)
topic:
Sample topic:
Topic ideas:
Preprocess the hash values from 1 to all lengths first, and store them in the multiplied prefix and array (the timeout will occur without preprocessing). When querying, take sum[b]/sum[a-1], which is the hash value from a to b. When dividing, inverse element processing is required.
AC:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+5;
int n,a,b,x,y;
int sum[N],ans[N];
string s;
int ex_gcd(int a,int b,int &x,int &y){
if(b==0){
x=1;
y=0;
return a;
}
int d=ex_gcd(b,a%b,x,y);
int tmp=x;
x=y;
y=tmp-(a/b)*y;
return d;
}
int main(){
while(cin>>n){
sum[0]=1;
ans[0]=1;
cin>>s;
int len=s.size();
for(int i=0;i<len;i++){
int nn=s[i];
sum[i+1]=sum[i]*(nn-28)%9973;
}
while(n--){
cin>>a>>b;
ex_gcd(sum[a-1],9973,x,y);
x=(x%9973+9973)%9973;
cout<<(sum[b]*x)%9973<<endl;
}
}
return 0;
}