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. flask的orm操作

    django是有orm操作的  可想而知 那么flask也是有orm操作的,其实flask的orm操作的使用和djnago的是差不多的 django的orm操作进行条件筛选的时候后面跟着的是objec ...

  2. AFNetworking2.0简易GET,POST请求封装以及使用

    AFNetworking2.0简易GET,POST请求封装以及使用 AFNetworking不用我赘述其强大性,本人仅仅做了非常简易的封装,解决了有时候请求出错的问题,提供源码给大家. 封装源码库下载 ...

  3. json格式转换(json,csjon)(天气预报)

    json格式数据默认为string,可以使用eval()函数或者json模块将其转换为dict.标准Json字符串必须使用双引号(")而不能使用单引号('),否则从字符串转换成dict类型会 ...

  4. Linux 文件的读写执行权限的说明

    文件的读写执行权限的说明 X 进入目录的权限: cd 1.文件本身是可执行的 2.普通用户还具备r的权限 3.root用户只需要有r的权限即可 r 查看目录/文件的内容 :ls dir 没有读的权 限 ...

  5. JS DateTime 格式化

    首先看我们在浏览器接收到的DateTime格式的数据: 如果没有在传输的时候把DateTime转成字符串的话,我们只需要在JS中加一段代码即可转换,代码如下: function ChangeDateF ...

  6. 一、MySQL中的索引 二、MySQL中的函数 三、MySQL数据库的备份和恢复 四、数据库设计和优化(重点)

    一.MySQL中的索引###<1>索引的概念 索引就是一种数据结构(高效获取数据),在mysql中以文件的方式存在.存储建立了索引列的地址或者指向. 文件 :(以某种数据 结构存放) 存放 ...

  7. JavaScript创建对象的6种方式

    JavaScript创建对象简单的说,无非就是使用内置对象(Object)或各种自定义对象,当然还可以用JSON,但写法有很多种,也能混合使用. 1.对象字面量的方式 person = {name : ...

  8. async/await actor promise 异步编程

    Python协程:从yield/send到async/await http://blog.guoyb.com/2016/07/03/python-coroutine/ Async/Await替代Pro ...

  9. linux_bc命令

    bc 命令:     bc 命令是用于命令行计算器. 它类似基本的计算器. 使用这个计算器可以做基本的数学运算. 语法:  语法是      bc [命令开关]命令开关:      -c 仅通过编译. ...

  10. P2245 星际导航

    题目描述 sideman 做好了回到 Gliese星球的硬件准备,但是 sideman 的导航系统还没有完全设计好.为了方便起见,我们可以认为宇宙是一张有 N 个顶点和 M 条边的带权无向图,顶点表示 ...