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 ;
}

4542: [Hnoi2016]大数的更多相关文章

  1. BZOJ.4542.[HNOI2016]大数(莫队)

    题目链接 大数除法是很麻烦的,考虑能不能将其条件化简 一段区间[l,r]|p,即num[l,r]|p,类似前缀,记后缀suf[i]表示[i,n]的这段区间代表的数字 于是有 suf[l]-suf[r+ ...

  2. 【BZOJ】4542: [Hnoi2016]大数

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4542 给定一个由数字构成的字符串${S_{1,2,3,...,n}}$,一个正素数$P$, ...

  3. bzoj 4542 [Hnoi2016]大数 (坑)

    题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4542 题解 Code #include<bits/stdc++.h> using ...

  4. 4542: [Hnoi2016]大数

    Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小B还有一个素数P.现在,小 B 提出了 M 个询问,每个 ...

  5. bzoj 4542: [Hnoi2016]大数

    Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345 小B还有一个素数P.现在,小 B 提出了 M 个询问,每个 ...

  6. bzoj 4542: [Hnoi2016]大数 (莫队)

    Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小B还有一个素数P.现在,小 B 提出了 M 个询问,每个 ...

  7. 【LG3245】[HNOI2016]大数

    [LG3245][HNOI2016]大数 题面 洛谷 题解 60pts 拿vector记一下对于以每个位置为右端点符合要求子串的左端点, 则每次对于一个询问,扫一遍右端点在vector里面二分即可, ...

  8. 【BZOJ4542】[Hnoi2016]大数 莫队

    [BZOJ4542][Hnoi2016]大数 Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小B还有一个 ...

  9. 【bzoj4542】 Hnoi2016—大数

    http://www.lydsy.com/JudgeOnline/problem.php?id=4542 (题目链接) 题意 给出一个素数$P$,一个数串$S$,$m$个询问,每次询问区间$[l,r] ...

随机推荐

  1. JBoss jmx-console中的秘密

    JBoss jmx-console中的秘密 https://wenku.baidu.com/view/fe196f047cd184254b35351d.html

  2. [翻译] LiquidFloatingActionButton

    LiquidFloatingActionButton https://github.com/yoavlt/LiquidFloatingActionButton LiquidFloatingAction ...

  3. 如何在 OS X 中安装 ruby

    如何在 OS X 中安装 ruby 软件包下载 链接: http://pan.baidu.com/s/1mgMil5I 密码: tiqg 安装 http://brew.sh/ 1. 先安装 brew( ...

  4. 剑指offer 08跳台阶

    一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). java版本: public class Solution { public s ...

  5. windows server 2016部署服务

          绪论 有时候,如果安装系统数量比较多,而且数量较多,人工使用U盘或者光盘安装,效率较低,通过网络可以批量安装. 还可以做到无人安装. PXE PXE(preboot execute env ...

  6. $.ajax 在请求没有完成,是可以往下继续执行js代码的

    $.ajax({ url:url, data:{}, success:function(arr) { var varHtml='<option value="" checke ...

  7. ZT 复杂的函数指针例子分析 2008

     复杂的函数指针例子分析 2008-01-26 11:38:22 分类: 一个最简单的函数指针定义如下: Void (*funcPtr) (); //funcptr是一个函数指针,它指向的函数没有参数 ...

  8. Leuze BCL308i 使用方法整理

    1 硬件连接关系 1.1 接口盒 BCL308i一般选配MK308/MK348/MK358系列接口盒, 单独使用(不组成扫描集群)时需要连接3根线,分别为SERVICE .SW/PWR.HOST/BU ...

  9. [webpack] Webpack 别名

    存在这样一种情况,有时候项目中,存在一些 公共的组件,通常会抽取出来,放在一个统一的文件夹中. 然后大家就可以再 各个 模块里面 愉快的使用该 组件了.   但是也带来一个坑爹的问题 组件放在 com ...

  10. CentOS7 防火墙(firewall)的操作命令

    CentOS7 防火墙(firewall)的操作命令 安装:yum install firewalld 1.firewalld的基本使用 启动: systemctl start firewalld 查 ...