本文链接http://www.cnblogs.com/Ash-ly/p/5494618.html

题意:

  度熊手上有一本字典存储了大量的单词,有一次,他把所有单词组成了一个很长很长的字符串。现在麻烦来了,他忘记了原来的字符串都是什么,神奇的是他竟然记得原来那些字符串的哈希值。一个字符串的哈希值,由以下公式计算得到:

请帮助度熊计算大字符串中任意一段的哈希值是多少。多组测试数据,每组测试数据第一行是一个正整数N,代表询问的次数,第二行一个字符串,代表题目中的大字符串,接下来N行,每行包含两个正整数a和b,代表询问的起始位置以及终止位置。

思路:
  首先利用一个前缀数组A[i]表示从第一个字符到第i个字符的字符串的H(s)值,如果要求从a到b,那么很显然是A[b] / A[a-1].又因为A数组是对9973取模的,所以相当于已知A[b] % P, A[a - 1] % P,求A[b] / A[a - 1].
已知 a % mod, b % mod, 求(a / b) % mod,用Inv(p)代表p的逆
(a / b) % mod = (a *  Inv(b)) % mod = (a % mod * Inv(b) % mod) % mod;
然后求 1 到 mod - 1 对 mod 逆元记为Inv(b % mod)
所以 (a / b) % mod = (a % mod * Inv(b % mod)) % mod;
求 1 到 mod - 1 对 mod 逆元的代码:
 const int mod = ;
LL Inverse[mod + ];
Inverse[] = , Inverse[] = ;
for(int i = ; i < mod; i++)
Inverse[i] = ( - ( mod / i ) * Inverse[ mod % i ] ) % mod + mod;

这道题代码:

 const int maxN =  + ;
const int p = ;
LL ni[p];
char str[maxN];
LL a[maxN]; int main(){
//freopen("input.txt", "r", stdin);
ni[] = ;
ni[] = ;
for(int i = ; i < p; i++)
ni[i] = (-(p/i) * ni[p%i]) % p + p;
int n;
while(~scanf("%d", &n))
{
scanf("%s", str);
int len = strlen(str);
a[] = ;
for(int i = ; i <= len; i++)
a[i] = a[i - ] * (str[i - ] - ) % p;
while(n--){
int s, m;
scanf("%d%d", &s, &m);
printf("%I64d\n", a[m] * ni[a[s - ]] % p);
}
}
return ;
}

百度之星资格赛 2016 Problem 1001的更多相关文章

  1. 百度之星资格赛 2016 Problem 1004

    本文链接:http://www.cnblogs.com/Ash-ly/p/5494630.html 题意: 熊所居住的 D 国,是一个完全尊重人权的国度.以至于这个国家的所有人命名自己的名字都非常奇怪 ...

  2. 百度之星资格赛 2016 Problem 1002

    本文链接:http://www.cnblogs.com/Ash-ly/p/5494623.html 题意: 度熊面前有一个全是由1构成的字符串,被称为全1序列.你可以合并任意相邻的两个1,从而形成一个 ...

  3. HDU 5688:2016"百度之星" - 资格赛 Problem D

    原文链接:https://www.dreamwings.cn/hdu5688/2650.html Problem D Time Limit: 2000/1000 MS (Java/Others)    ...

  4. HDU 5686:2016"百度之星" - 资格赛 Problem B

    原文链接:https://www.dreamwings.cn/hdu5686/2645.html Problem B Time Limit: 2000/1000 MS (Java/Others)    ...

  5. HDU 5685:2016"百度之星" - 资格赛 Problem A

    原文链接:https://www.dreamwings.cn/hdu5685/2637.html Problem A Time Limit: 2000/1000 MS (Java/Others)    ...

  6. 2016百度之星 资格赛ABCDE

    看题:http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=690 交题:http://acm.hdu.edu.cn/search.php ...

  7. 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)

    .navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...

  8. 模拟 2015百度之星资格赛 1003 IP聚合

    题目传送门 /* 模拟水题,排序后找出重复的ip就可以了 */ #include <cstdio> #include <iostream> #include <algor ...

  9. 模拟 百度之星资格赛 1003 IP聚合

    题目传送门 /* 模拟水题,排序后找出重复的ip就可以了 */ #include <cstdio> #include <iostream> #include <algor ...

随机推荐

  1. 【学习笔记】Learning OpenCV3——Ch8 working with video

    Reading Video with the cv::VideoCapture Object 对象创建的三种方法: // 1. Input filename cv::VideoCapture::Vid ...

  2. [Leetcode] subsets 求数组所有的子集

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  3. [Leetcode] remove element 删除元素

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  4. 【NOIP模拟赛】就 反悔贪心

    biubiu~~~ 这道题,考场上上来就dp然后发现怎么优化也不行.............最后发现是贪心............. 正解:带反悔的贪心,原理是,假设我们现在得到了取i个的最优解那么我 ...

  5. git使用笔记(七)版本回退和撤销

    By francis_hao    Nov 21,2016 从版本库初始化开始,每一步的撤销操作 添加第一个文件 在空的版本库中创建了一个文件并git add到了缓存区,这时候怎么撤销呢? 撤销单个文 ...

  6. Django随笔 01

    Django 视图 不处理用户输入,而仅仅决定要展现哪些数据给用户: Django 模板 仅仅决定如何展现Django视图指定的数据. dd http://blog.csdn.net/pipisorr ...

  7. PHP正则替换preg_replace函数的使用

    <?php $str="as2223adfsf0s4df0sdfsdf"; echo preg_replace("/0/","",$s ...

  8. springmvc4+hibernate4+activiti5.18(Maven)

    项目下载地址: http://files.cnblogs.com/files/walk-the-Line/springmvc_activiti5.18_hibernate4.zip

  9. ZOJ3872 Beauty of Array---规律 | DP| 数学能力

    传送门ZOJ 3872 Beauty of Array Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward has an array A  ...

  10. 【BZOJ3942】Censoring [KMP]

    Censoring Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 有一个S串和一个T串,长 ...