ZOJ 3940 Modulo Query(YY+二分)
Modulo Query
Time Limit: 2 Seconds Memory Limit: 65536 KB
One day, Peter came across a function which looks like:
- F(1, X) = X mod A1.
- F(i, X) = F(i - 1, X) mod Ai, 2 ≤ i ≤ N.
Where A is an integer array of length N, X is a non-negative integer no greater than M.
Peter wants to know the number of solutions for equation F(N, X) = Y, where Y is a given number.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains two integers N and M (2 ≤ N ≤ 105, 0 ≤ M ≤ 109).
The second line contains N integers: A1, A2, ..., AN (1 ≤ Ai ≤ 109).
The third line contains an integer Q (1 ≤ Q ≤ 105) - the number of queries. Each of the following Q lines contains an integer Yi (0 ≤ Yi ≤ 109), which means Peter wants to know the number of solutions for equation F(N, X) = Yi.
Output
For each test cases, output an integer S = (1 ⋅ Z1 + 2 ⋅ Z2 + ... + Q ⋅ ZQ) mod (109 + 7), where Zi is the answer for the i-th query.
Sample Input
1
3 5
3 2 4
5
0
1
2
3
4
Sample Output
8
Hint
The answer for each query is: 4, 2, 0, 0, 0.
题目链接:ZOJ 3940
题意:给出N个数Ai和M,又给Q个询问,每一个询问都是求[0,M]中求是否存在X使得X%A1%A2%A3%......%An=Yi,输出符合有几个这种整数X。
可以发现任何情况下对连续的数取模除非当前值比上一个取模值小,否则直接跳过即可,当然最重要的不是这里,而是如何把题目转换一下,每一次问[0,M]中符合题意的X个数,那么我们可以把[0,M]区间分割为无数个被取模后的小区间,若计这些小区间的贡献均为1,则覆盖在点Yi的情况就是询问Yi的答案,怎么分割呢,当然是用题目给的A数组分割,顺序地输入数组,这里就可以用到上面讲到的取模的技巧来减少分割的次数,分割之后做一遍前缀或后缀和(比如小区间0-1与0-2,0-2显然是包括0-1的,因此是前缀或后缀和关系,两种不同的和只会影响统计时候的加减法问题不会影响答案)。然后询问的时候二分到第一个大于Yi的区间,假设你输入的是3,二分出来的位置是pos,pos对应的子区间为0-4,答案就是从pos~end的贡献和。因为在pos之前只会产生小于3的数,不可能出现3的情况,只有从至少%4开始,才会出现3
代码:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
typedef pair<int, int> pii;
typedef long long LL;
const int N = 1e5 + 7;
const LL MOD = 1e9 + 7;
int A[N];
map<int, int>pos;
pii prefix[N << 2]; inline int getpos(int l, int r, int key)
{
int ans = -1;
while (l <= r)
{
int mid = (l + r) >> 1;
if (prefix[mid].first > key)
{
ans = mid;
r = mid - 1;
}
else
l = mid + 1;
}
return ans;
}
int main(void)
{
int tcase, n, m, i, q;
scanf("%d", &tcase);
while (tcase--)
{
int Min = INF;
pos.clear();
scanf("%d%d", &n, &m);
pos[m + 1] = 1;
prefix[0] = {0, 0};
for (i = 1; i <= n; ++i)
{
scanf("%d", &A[i]);
if (A[i] < Min)
Min = A[i];
while (1)
{
auto it = pos.upper_bound(A[i]);
if (it == pos.end())
break;
int olde = it->first;
int oldcnt = it->second;
pos[A[i]] += olde / A[i] * oldcnt;
if (olde % A[i] != 0)
pos[olde % A[i]] += oldcnt;
pos.erase(it);
}
}
auto it = pos.begin();
int sz = 0;
while (it != pos.end())
{
++sz;
prefix[sz].second = prefix[sz - 1].second + it->second;
prefix[sz].first = it->first;
++it;
}
scanf("%d", &q);
LL ans = 0LL;
for (i = 1; i <= q; ++i)
{
LL curans;
int x;
scanf("%d", &x);
if (x >= Min)
curans = 0LL;
else
{
int l = getpos(1, sz, x);
if (~l)
curans = prefix[sz].second - prefix[l - 1].second;
else
curans = 0LL;
}
if (curans)
{
ans = ans + (LL)i * curans % MOD;
if (ans > MOD)
ans %= MOD;
}
}
printf("%lld\n", ans);
}
return 0;
}
ZOJ 3940 Modulo Query(YY+二分)的更多相关文章
- ZOJ 3940 Modulo Query
0--M对某个数字取模,相当于把0--M区间进行切割,每次暴力切割一下.结果的算的时候二分一下即可... 看了官方题解才会... #include<cstdio> #include< ...
- ZOJ 3940 Modulo Query (2016年浙江省赛E题,区间折叠 + map运用)
题目链接 2016 ZJCPC Problem E 考虑一个开区间$[0, x)$对$a_{i}$取模的过程. $[0, x)$中小于$a_{i}$的部分不变,大于等于$a_{i}$的部分被切下来变 ...
- ZOJ 3911 Prime Query ZOJ Monthly, October 2015 - I
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a s ...
- ZOJ 3911 Prime Query(线段树)
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a s ...
- ZOJ 4062 - Plants vs. Zombies - [二分+贪心][2018 ACM-ICPC Asia Qingdao Regional Problem E]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4062 题意: 现在在一条 $x$ 轴上玩植物大战僵尸,有 $n$ ...
- 2018 青岛ICPC区域赛E ZOJ 4062 Plants vs. Zombie(二分答案)
Plants vs. Zombies Time Limit: 2 Seconds Memory Limit: 65536 KB BaoBao and DreamGrid are playin ...
- zoj 2362 Beloved Sons【二分匹配】
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2361 来源:http://acm.hust.edu.cn/vjudg ...
- ZOJ 5638——Prime Query——————【线段树区间更新,区间查询,单点更新】
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a s ...
- ZOJ 2112 Dynamic Rankings(二分,树套树)
动态区间询问kth,单点修改. 区间用线段树分解,线段树上每条线段存一颗平衡树. 不能直接得到kth,但是利用val和比val小的个数之间的单调性,二分值.log^3N. 修改则是一次logN*log ...
随机推荐
- flutter 踩坑总结
导入第三方库踩坑小结: (编译器:VsCode) ( 打算在学习中,使用flutter重新自己的项目,遇到比较特殊的坑,就先记录一下,持续更新中) 1.把第三方库 写入pubspec.yaml文件中 ...
- 人脸验证算法Joint Bayesian详解及实现(Python版)
人脸验证算法Joint Bayesian详解及实现(Python版) Tags: JointBayesian DeepLearning Python 本博客仅为作者记录笔记之用,不免有很多细节不对之处 ...
- ssh: connect to host localhost port 22: Connection refused
1.hadoop安装好之后,执行ssh localhost无法执行, 提示“ssh: connect to host localhost port 22: Connection refused”. 2 ...
- Java 性能优化的五大技巧
要对你的 Java 代码进行优化,需要理解 Java 不同要素之间的相互作用,以及它是如何与其运行时的操作系统进行交互的.使用下面这五个技巧和资源,开始学习如何分析和优化你的代码吧. 在我们开始之前, ...
- strong和weak
ios中使用ARC后,内存管理使用了新的关键字:strong(强引用) 和 weak(弱引用),默认是strong引用 strong: 使用strong类型指针指向的对象,会一直保持指向,直到所有st ...
- 处理侧滑返回与 ScrollView 手势冲突
与处理双击.单击手势互斥原则一样: // 手势互斥(侧滑返回手势失效后才响应UITableView的滑动手势) [tableView.panGestureRecognizer requireGestu ...
- unix环境高级编程一书中部分错误处理函数
#include <unistd.h> #include <errno.h> #include <string.h> #include <stdio.h> ...
- Java - 若try中有return语句,finally会执行吗?在return之前还是之后呢?
会执行,在方法return动作之前,return语句执行之后,若finally中再有return语句,则此方法以finally的return作为最终返回,若finally中无return语句,则此方法 ...
- PKI
公钥基础设施(Public Key Infrastructure,简称PKI)是眼下网络安全建设的基础与核心,是电子商务安全实施的基本保障,因此,对PKI技术的研究和开发成为眼下信息安全领域的热点.本 ...
- 科学计算库Numpy——排序
矩阵按维度排序 使用np.sort()进行排序. 排序索引值 使用np.argsort()排序,返回排序后的索引值. 备注:array1[1,2]=1.2,array1[1,0]=5.6,array1 ...