4542: [Hnoi2016]大数
Description
小 B 有一个很大的数 S,长度达到了 N 位;这个数可以看成是一个串,它可能有前导 0,例如00009312345
。小B还有一个素数P。现在,小 B 提出了 M 个询问,每个询问求 S 的一个子串中有多少子串是 P 的倍数(0 也
是P 的倍数)。例如 S为0077时,其子串 007有6个子串:0,0,7,00,07,007;显然0077的子串007有6个子串都是素
数7的倍数。
Input
第一行一个整数:P。第二行一个串:S。第三行一个整数:M。接下来M行,每行两个整数 fr,to,表示对S 的
子串S[fr…to]的一次询问。注意:S的最左端的数字的位置序号为 1;例如S为213567,则S[1]为 2,S[1…3]为 2
13。N,M<=100000,P为素数
Output
输出M行,每行一个整数,第 i行是第 i个询问的答案。
Sample Input
121121
3
1 6
1 5
1 4
Sample Output
3
2
//第一个询问问的是整个串,满足条件的子串分别有:121121,2112,11,121,121。
HINT
2016.4.19新加数据一组
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long int64;
char ch;
bool ok;
void read(int &x){
ok=;
for (ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
void read(int64 &x){
ok=;
for (ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
const int maxn=;
char s[maxn];
int n,q,sn,x,y,bel[maxn],tot,cnt[maxn];
int64 mod,a[maxn],ans[maxn],res,pw[maxn],list[maxn];
struct Querys{
int l,r,id;
void init(int i){read(l),read(r),l=n-l+,r=n-r+,swap(l,r),l--,/*cout<<"FFFF "<<l<<' '<<r<<endl,*/id=i;}
}querys[maxn];
bool cmp(const Querys &a,const Querys &b){return bel[a.l]<bel[b.l]||(bel[a.l]==bel[b.l]&&bel[a.r]<bel[b.r]);}
void modify(int col,int op){
if (op==) /*cout<<res<<' '<<col<<' '<<cnt[col]<<endl,*/res+=cnt[col],cnt[col]++;
else cnt[col]--,res-=cnt[col];
}
void work(){
int l=,r=; cnt[a[]]=,res=;
for (int i=;i<=q;i++){
for (;l<querys[i].l;l++) modify(a[l],-);
for (;l>querys[i].l;l--) modify(a[l-],);
for (;r<querys[i].r;r++) modify(a[r+],);
// cout<<"res="<<res<<endl;
for (;r>querys[i].r;r--) modify(a[r],-);
ans[querys[i].id]=res;
}
}
int cnt2[maxn],cnt5[maxn];
int64 ans2[maxn],ans5[maxn];
int main(){
read(mod);
if (mod==){
scanf("%s",s+),n=strlen(s+);
for (int i=;i<=n;i++){
cnt2[i]=cnt2[i-],ans2[i]=ans2[i-];
if ((s[i]-'')%==) cnt2[i]++,ans2[i]+=i;
}
for (read(q);q;q--){
read(x),read(y);
printf("%lld\n",ans2[y]-1LL*(cnt2[y]-cnt2[x-])*(x-)-ans2[x-]);
}
}
else if (mod==){
scanf("%s",s+),n=strlen(s+);
for (int i=;i<=n;i++){
cnt5[i]=cnt5[i-],ans5[i]=ans5[i-];
if ((s[i]-'')%==) cnt5[i]++,ans5[i]+=i;
}
for (read(q);q;q--){
read(x),read(y);
printf("%lld\n",ans5[y]-1LL*(cnt5[y]-cnt5[x-])*(x-)-ans5[x-]);
}
}
else{
scanf("%s",s+),n=strlen(s+),sn=sqrt(n);
for (int i=,j=n;i<j;i++,j--) swap(s[i],s[j]);
for (int i=;i<=n;i++) bel[i]=i/sn;
// for (int i=0;i<=n;i++) cout<<i<<' ';cout<<endl;
// for (int i=0;i<=n;i++) cout<<bel[i]<<' ';cout<<endl;
pw[]=;
for (int i=;i<=n;i++) pw[i]=1LL*pw[i-]*%mod;
for (int i=;i<=n;i++) a[i]=(a[i-]+1LL*(s[i]-'')*pw[i-]%mod)%mod;
for (int i=;i<=n;i++) list[++tot]=a[i];
sort(list+,list+tot+),tot=unique(list+,list+tot+)-list-;
// for (int i=0;i<=n;i++) cout<<a[i]<<' ';cout<<endl;
for (int i=;i<=n;i++) a[i]=lower_bound(list+,list+tot+,a[i])-list;
// for (int i=0;i<=n;i++) cout<<a[i]<<' ';cout<<endl;
read(q);
// cout<<q<<endl;
for (int i=;i<=q;i++) querys[i].init(i);
// for (int i=1;i<=q;i++) cout<<querys[i].l<<' '<<querys[i].r<<' '<<querys[i].id<<endl;
sort(querys+,querys+q+,cmp);
work();
for (int i=;i<=q;i++) printf("%lld\n",ans[i]);
}
return ;
}
/*
11
121121
3
1 6
1 5
1 4
*/
4542: [Hnoi2016]大数的更多相关文章
- 4542: [Hnoi2016]大数
4542: [Hnoi2016]大数 链接 分析: 如果p等于2或者5,可以根据最后一位直接知道是不是p的倍数,所以直接记录一个前缀和即可. 如果p不是2或者5,那么一个区间是p的倍数,当且仅当$\f ...
- BZOJ.4542.[HNOI2016]大数(莫队)
题目链接 大数除法是很麻烦的,考虑能不能将其条件化简 一段区间[l,r]|p,即num[l,r]|p,类似前缀,记后缀suf[i]表示[i,n]的这段区间代表的数字 于是有 suf[l]-suf[r+ ...
- 【BZOJ】4542: [Hnoi2016]大数
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4542 给定一个由数字构成的字符串${S_{1,2,3,...,n}}$,一个正素数$P$, ...
- bzoj 4542 [Hnoi2016]大数 (坑)
题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4542 题解 Code #include<bits/stdc++.h> using ...
- bzoj 4542: [Hnoi2016]大数
Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345 小B还有一个素数P.现在,小 B 提出了 M 个询问,每个 ...
- bzoj 4542: [Hnoi2016]大数 (莫队)
Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小B还有一个素数P.现在,小 B 提出了 M 个询问,每个 ...
- 【LG3245】[HNOI2016]大数
[LG3245][HNOI2016]大数 题面 洛谷 题解 60pts 拿vector记一下对于以每个位置为右端点符合要求子串的左端点, 则每次对于一个询问,扫一遍右端点在vector里面二分即可, ...
- 【BZOJ4542】[Hnoi2016]大数 莫队
[BZOJ4542][Hnoi2016]大数 Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小B还有一个 ...
- 【bzoj4542】 Hnoi2016—大数
http://www.lydsy.com/JudgeOnline/problem.php?id=4542 (题目链接) 题意 给出一个素数$P$,一个数串$S$,$m$个询问,每次询问区间$[l,r] ...
随机推荐
- lcx源代码以及免杀的研究
之前和Random大神讨论了一下免杀的问题,他给出了一个比较不错的想法,使用debug版本发布可以过很多杀软.顺便看了下lcx的源码,发现其代码不算特别复杂,于是乎就在这分析一下. 报毒情况 因为使用 ...
- Ubuntu vim+ ctags(包含系统函数) + taglist 配置 分类: vim ubuntu 2015-06-09 18:19 195人阅读 评论(0) 收藏
阅读大型代码,我们经常需要打开很多的代码文件,搜索各种定义.windows下用惯了ide的朋友,转战Linux的时候可能会觉得很难受,找不到合适的阅读工具.其实万能的vim就可以实现.下面介绍一下vi ...
- 用apache的httpclient发请求和接受数据
此处发请求的是用httpclient4,请自己下载所需要的jar包. 发post请求,并得到数据. String url = "http://localhost:8080/lee" ...
- 【Java基础】Jar包结构结构分析和操作具体解释
作者:郭嘉 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.com/AllenWell 一 ...
- (亲测)设置myeclipse打开默认工作空间
亲测一: 1.找到D:\MyEclipse 8.5\configuration\ config.ini 这个文件 2.找到这一行instance.area.default 3.将后面的地址替换为你想要 ...
- 基于bootstrap面板的类别多选栏
1.html部分 <div class="panel panel-default"> <div class="panel-heading"&g ...
- RedHat7 SELinux
SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统.NSA是在Linux社区的帮助下开发了一种 ...
- ASP.NET操作DataTable
.创建 datatable DataTable dt=new Datable();//可以给表创建一个名字,tb .给表加个列名: dt.Columns.Add("id", typ ...
- P2P金融的概念理解
P2P金融又叫P2P信贷.其中,P2P是 peer-to-peer 或 person-to-person 的简写, 意思是:个人对个人. P2P金融指个人与个人间的小额借贷交易,一般需要借助电子商务专 ...
- 训练趣题:黑与白 有A、B、C、D、E五人,每人额头上都帖了一张黑或白的纸。(此处用javascript实现)
今天的题目原题是这样的: “ 黑与白:有A.B.C.D.E五人,每人额头上都帖了一张黑或白的纸.五人对坐,每人都可以看到其它人额头上的纸的颜色.五人相互观察后,A说:“我看见有三人额头上帖的是白纸,一 ...