2016"百度之星" - 资格赛(Astar Round1)Problem A

来源:互联网 发布:电路图设计软件下载 编辑:程序博客网 时间:2024/05/18 00:45

A

题解

用 dp[i] 表示前 i 个字符的 hash 值,那么子串 Sa...b的 hash 值:
H(s)=dp[b]/dp[a1]%mod
嗯,这里1/dp[a1]%mod的计算是一个逆元。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100000 + 10;const int mod  = 9973;int   n, a, b;char  s[maxn];int   dp[maxn];int inv(int x, int y){    if(x == 1) return 1;    return inv(y % x, y) * (y - y / x) % y;}int main(){    while(scanf("%d", &n) != EOF){        scanf("%s", s);        int len = strlen(s);        dp[0] = 1;        for(int i = 1; i <= len; ++i){            dp[i] = dp[i - 1] * (s[i - 1] - 28) % mod;        }        for(int i = 0; i < n; ++i){            scanf("%d %d", &a, &b);            printf("%d\n", dp[b] * inv(dp[a - 1], mod) % mod);        }    }    return 0;}
0 0
原创粉丝点击