天天看點

HDU 5685 Problem A

Problem Description

度熊手上有一本字典存儲了大量的單詞,有一次,他把所有單詞組成了一個很長很長的字元串。現在麻煩來了,他忘記了原來的字元串都是什麼,神奇的是他竟然記得原來那些字元串的哈希值。一個字元串的哈希值,由以下公式計算得到:

H(s)=∏i≤len(s)i=1(Si−28) (mod 9973)

Si代表 S[i] 字元的 ASCII 碼。

請幫助度熊計算大字元串中任意一段的哈希值是多少。

Input

N,代表詢問的次數,第二行一個字元串,代表題目中的大字元串,接下來

N行,每行包含兩個正整數

a和

b,代表詢問的起始位置以及終止位置。

1≤N≤1,000

1≤len(string)≤100,000

1≤a,b≤len(string)

Output

a 位到 

b

Sample Input

2

ACMlove2015

1 11

8 10

1

testMessage

1 1

Sample Output

6891

9240

88

區間求積的問題,直接算出字首積,然後用逆元即可,據說線段樹什麼的會炸掉。。。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod = 9973;
const int maxn = 1e5 + 10;
int n, l, r, f[maxn], inv[maxn];
char s[maxn];

int main()
{
    inv[1] = 1;
    for (int i = 2; i < mod; i++) inv[i] = inv[mod%i] * (mod - mod / i) % mod;
    while (scanf("%d", &n) != EOF)
    {
        scanf("%s", s + 1);
        f[0] = 1;
        for (int i = 1; s[i]; i++) f[i] = f[i - 1] * (s[i] - 28) % mod;
        while (n--)
        {
            scanf("%d%d", &l, &r);
            printf("%d\n", f[r] * inv[f[l - 1]] % mod);
        }
    }
    return 0;
}