4542: [Hnoi2016]大数

链接

分析:

  如果p等于2或者5,可以根据最后一位直接知道是不是p的倍数,所以直接记录一个前缀和即可。

  如果p不是2或者5,那么一个区间是p的倍数,当且仅当$\frac{b[l] - b[r + 1]}{10 ^ {r - l + 1}} = 0 \ (mod \ p)$。

  由于p不是2或者5,所以10与p互质,条件转化为$b[r] - b[l] = 0 \ (mod \ p)$ ,于是将b离散化后,莫队即可。
代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
int p;
char s[N];
namespace BF1{
LL s1[N], s2[N];
void solve() {
scanf("%s",s + );
int n = strlen(s + );
for (int i = ; i <= n; ++i) {
s1[i] = s1[i - ], s2[i] = s2[i - ];
if ((s[i] - '') % p == ) s1[i] ++, s2[i] += i;
}
int m = read();
while (m --) {
int l = read(), r = read();
printf("%lld\n", s2[r] - s2[l - ] - 1ll * (l - ) * (s1[r] - s1[l - ]));
}
}
}
namespace BF2{
struct Que{ int l, r, bel, id; } Q[N];
bool operator < (const Que &A,const Que &B) { return A.bel == B.bel ? A.r < B.r : A.bel < B.bel; }
int a[N], cnt[N];
LL ans[N], b[N], disc[N];
void solve() {
scanf("%s", s + );
int n = strlen(s + ), B = sqrt(n);
for (int i = n, pw = ; i >= ; --i, pw = 1ll * pw * % p) // 开long long
disc[i] = b[i] = (1ll * (s[i] - '') * pw % p + b[i + ]) % p;
disc[n + ] = ;
sort(disc + , disc + n + );
int tot = ;
for (int i = ; i <= n + ; ++i) if (disc[i] != disc[tot]) disc[++tot] = disc[i];
for (int i = ; i <= n + ; ++i) a[i] = lower_bound(disc + , disc + tot + , b[i]) - disc;
int m = read();
for (int i = ; i <= m; ++i)
Q[i].l = read(), Q[i].r = read() + , Q[i].bel = (Q[i].l - ) / B + , Q[i].id = i;
sort(Q + , Q + m + );
int L = , R = ; LL now = ;
for (int i = ; i <= m; ++i) {
while (L > Q[i].l) L --, now += cnt[a[L]], cnt[a[L]] ++;
while (R < Q[i].r) R ++, now += cnt[a[R]], cnt[a[R]] ++;
while (L < Q[i].l) cnt[a[L]] --, now -= cnt[a[L]], L ++;
while (R > Q[i].r) cnt[a[R]] --, now -= cnt[a[R]], R --;
ans[Q[i].id] = now;
}
for (int i = ; i <= m; ++i) printf("%lld\n", ans[i]);
}
}
int main() {
p = read();
if (p == || p == ) BF1::solve();
else BF2::solve();
return ;
}

最新文章

  1. 好程序与差程序Good Programming, Bad Programming
  2. C-线性顺序表的增删改查
  3. hdu2874 LCA
  4. Sql的一些概念
  5. 使用WebView视图显示网页-----迷你浏览器
  6. Float精度 在JS的解决方法
  7. C# 按拼音/笔划 排序的简单示例(转)
  8. Android UI -- 布局介绍(布局包括FrameLayout, LinearLayout, RelativeLayout, GridLayout)
  9. Python脚本调用C#代码数据交互示例(hello world)
  10. UGUI之UI的深度问题
  11. 如何把一个表中的部分字段值插入到另一个表中去 这sql吊
  12. Bzoj4199:[NOI2015]品酒大会
  13. .Net Core小技巧 - 使用Swagger上传文件
  14. LYK loves graph(graph)
  15. HTTP Status 404 - No result defined for action com.ouyang.action.GreetingAction and result success 错误解决办法
  16. 第32课 Linux内核链表剖析
  17. FineBI学习系列之FineBI的Windows里安装步骤(图文详解)
  18. 循环插入oracle 存储过程
  19. HDU 1254 推箱子(BFS)
  20. mysql主从同步详细教程

热门文章

  1. Linq排序方式与Lambda排序方式比较以及OrderBy、ThenBy的使用
  2. 奇怪的等待事件“enq: ss - contention”
  3. iOS设计模式 - 模板
  4. 设置邮箱发送服务|邮箱开始SMTP服务和腾讯云解封25端口的经验总结
  5. XXX esx.problem.hyperthreading.unmitigated.formatOnHost not found XXX (Build 9313334)
  6. 数据库初始化以及制作为Windows服务
  7. 3-urllib的post请求方式
  8. ArcGIS Engine开发基础总结(一)
  9. 使用transient关键字解决ehcache序列化错误
  10. 【洛谷】【treap/堆】P2073 送花