ZOJ 3940 Modulo Query
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.
Author: LIN, Xi
Source: The 13th Zhejiang Provincial Collegiate Programming Contest
没想到是这么暴力的题,比赛的时候没敢做。。。
#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int mod = 1e9 + 7;
const int maxn = 4e5 + 10;
int T, q, n, m, x, sum[maxn], b[maxn], ans, sz;struct point
{int x, y;point(int x = 0, int y = 0) :x(x), y(y) {}bool operator<(const point&a)const{return x < a.x;}
}a[maxn];int main()
{scanf("%d", &T);while (T--){scanf("%d%d", &n, &m);priority_queue<point> p;p.push(point(m, 1));while (n--){scanf("%d", &x);while (p.top().x >= x){point q = p.top(); p.pop();while (!p.empty() && p.top().x == q.x) q.y += p.top().y, p.pop();p.push(point(x - 1, (q.x + 1) / x*q.y));if ((q.x + 1) % x)p.push(point(q.x%x, q.y));}}sz = 1; ans = 0;while (!p.empty()){point q = p.top(); p.pop();while (!p.empty() && p.top().x == q.x) q.y += p.top().y, p.pop();a[sz++] = q;}sort(a + 1, a + sz);for (int i = 1; i < sz; i++){sum[i] = sum[i - 1] + a[i].y;b[i] = a[i].x;}scanf("%d", &q);for (int i = 1; i <= q; i++){scanf("%d", &x);int k = lower_bound(b + 1, b + sz, x) - b;(ans += (LL)(sum[sz - 1] - sum[k - 1])*i%mod) %= mod;}printf("%d\n", ans);}return 0;
}
ZOJ 3940 Modulo Query相关推荐
- 【2016浙江省赛:区间取模】E : Modulo Query | ZOJ - 3940
2016浙江省赛:E 题 Modulo Query [难度] 4.5/104.5/104.5/10 据说是卡银题?感觉有点难 [题意] F(i,X)={XmodA1i=1F(i−1,X)modAi2≤ ...
- ZOJ 3597 Hit the Target! (线段树扫描线 -- 矩形所能覆盖的最多的点数)
ZOJ 3597 题意是说有n把枪,有m个靶子,每把枪只有一发子弹(也就是说一把枪最多只能打一个靶子), 告诉你第 i 把枪可以打到第j个靶, 现在等概率的出现一个连续的P把枪,在知道这P把枪之后,你 ...
- zoj - 2112 带修改主席树 + 空间优化
ZOJ - 2112 题意:求区间第k小 思路:带修改区间第k小裸题,无修改的主席树是维护一个前缀线段树,每次更新log个节点,用root 和 ls rs作为每颗前缀线段树的根节点和左右子树的索引(相 ...
- 【SPOJ】Power Modulo Inverted(拓展BSGS)
[SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- 思维+multiset ZOJ Monthly, July 2015 - H Twelves Monkeys
题目传送门 1 /* 2 题意:n个时刻点,m次时光穿梭,告诉的起点和终点,q次询问,每次询问t时刻t之前有多少时刻点是可以通过两种不同的路径到达 3 思维:对于当前p时间,从现在到未来穿越到过去的是 ...
- POJ 2777 ZOJ 1610 HDU 1698 --线段树--区间更新
直接将这3题 放一起了 今天在做线段树的东西 这3个都是区间更新的 查询方式互相不同 反正都可以放到一起吧 直接先上链接了 touch me touch me touch me 关于涉及到区间的修改 ...
- ZOJ 3789 Gears
并查集, 删除节点操作,可以用新建节点代替 维护每个点到跟节点的距离 Gears Time Limit: 2 Seconds Memory Limit: 65536 KB Bob has ...
- ZOJ 2833 Friendship
ZOJ 2833 Friendship(并查集) 题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827366332 Fr ...
最新文章
- Centos下机器学习算法Mahout库的安装和示例
- Javascript - Vue - webpack
- 航天总线测试系统软件,总线仿真测试平台软件
- Cocos2dx学习笔记(1) Ref类型数据 垃圾回收机制
- kalman filter卡尔曼滤波器- 数学推导和原理理解-----网上讲的比较好的kalman filter和整理、将预测值和观测值融和...
- Linux下SHELL的PS1变量简介
- STM32工作笔记0097---OEM厂是什么意思
- Deep learning 学习开篇
- 如何查找MySQL中查询慢的SQL语句
- 安卓recovery流程分析【第一篇】
- 怎么轻松学JAVA(三个月拿实习Offer):小猿的JAVA后端之路(持续更新)
- BugKu 旋转跳跃(mp3stego(mp3隐写工具)的使用)
- AUtoCAD Civil 3D-曲面-原始数据处理
- java jdbc 批处理_JDBC的批处理操作
- k8s中的端口hostPort、port、nodePort、targetPort
- python绘制capm模型
- linux服务器硬件配置,linux服务器硬件配置要求是多少
- [SIGMOD 2021] SharPer: Sharding Permissioned Blockchains Over Network Clusters
- 排列显示阿拉伯语、数字及英文时的处理方法
- Spring学习笔记(一):眼见为实,先上一个简单例子