# 度熊的哈希 hdu5685

H(s)=∏ (Si−28) (mod 9973)

Si代表 S[i] 字符的 ASCII 码, ∏代表连乘，1<=i<=len(s)。

2
ACMlove2015
1 11
8 10
1
testMessage
1 1

6891
9240
88

Chr(2)=50, Chr(0)=48, Chr(1)=49

1≤N≤1,000

1≤len(string)≤100,000

1≤a,b≤len(string)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define MOD 9973
#define p2 (p<<1)
#define p3 ((p<<1)|1)
int n,len;
char s[100005];
int a[100005],tr[400005];
void build(int st,int ed,int p)
{
if (st==ed)
{
tr[p]=a[st];
return;
}
int mid=(st+ed)>>1;
build(st,mid,p2);
build(mid+1,ed,p3);
tr[p]=(tr[p2]*tr[p3])%MOD;
}
int query(int st,int ed,int l,int r,int p)
{
if (st==l && ed==r)
return tr[p];
int mid=(st+ed)>>1,ret;
if (r<=mid)
ret=query(st,mid,l,r,p2)%MOD;
else if (l>=mid+1)
ret=query(mid+1,ed,l,r,p3)%MOD;
else ret=query(st,mid,l,mid,p2)*query(mid+1,ed,mid+1,r,p3)%MOD;
return ret;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
scanf("%s",s+1);
len=strlen(s+1);
for (int i=1;i<=len;i++)
a[i]=(int)s[i]-28;
build(1,len,1);
for (int i=1;i<=n;i++)
{
int l,r;
scanf("%d%d",&l,&r);
if (l>r) swap(l,r);
printf("%d\n",query(1,len,l,r,1));
}
}
return 0;
}

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define MOD 9973
#define p2 (p<<1)
#define p3 ((p<<1)|1)
int n,len;
char s[100005];
int a[100005];
int qp(int x,int y)
{
int ret=1;
while (y)
{
if (y&1) ret=(ret*x)%MOD;
x=(x*x)%MOD;
y>>=1;
}
return ret;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
scanf("%s",s+1);
len=strlen(s+1);
a[0]=1;
for (int i=1;i<=len;i++)
a[i]=a[i-1]*((int)s[i]-28)%MOD;
for (int i=1;i<=n;i++)
{
int l,r;
scanf("%d%d",&l,&r);
if (r<l) swap(r,l);
printf("%d\n",a[r]*qp(a[l-1],MOD-2)%MOD);
}
}
return 0;
}