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(iX) = F(i - 1, X) mod Ai, 2 ≤ i ≤ N.

Where  A  is an integer array of length  NX  is a non-negative integer no greater than  M .

Peter wants to know the number of solutions for equation F(NX) = 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: A1A2, ..., 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(NX) = 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相关推荐

  1. 【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≤ ...

  2. ZOJ 3597 Hit the Target! (线段树扫描线 -- 矩形所能覆盖的最多的点数)

    ZOJ 3597 题意是说有n把枪,有m个靶子,每把枪只有一发子弹(也就是说一把枪最多只能打一个靶子), 告诉你第 i 把枪可以打到第j个靶, 现在等概率的出现一个连续的P把枪,在知道这P把枪之后,你 ...

  3. zoj - 2112 带修改主席树 + 空间优化

    ZOJ - 2112 题意:求区间第k小 思路:带修改区间第k小裸题,无修改的主席树是维护一个前缀线段树,每次更新log个节点,用root 和 ls rs作为每颗前缀线段树的根节点和左右子树的索引(相 ...

  4. 【SPOJ】Power Modulo Inverted(拓展BSGS)

    [SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...

  5. ZOJ 1610 Count the Colors (线段树区间更新)

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

  6. 思维+multiset ZOJ Monthly, July 2015 - H Twelves Monkeys

    题目传送门 1 /* 2 题意:n个时刻点,m次时光穿梭,告诉的起点和终点,q次询问,每次询问t时刻t之前有多少时刻点是可以通过两种不同的路径到达 3 思维:对于当前p时间,从现在到未来穿越到过去的是 ...

  7. POJ 2777 ZOJ 1610 HDU 1698 --线段树--区间更新

    直接将这3题 放一起了  今天在做线段树的东西 这3个都是区间更新的 查询方式互相不同 反正都可以放到一起吧 直接先上链接了 touch me touch me touch me 关于涉及到区间的修改 ...

  8. ZOJ 3789 Gears

    并查集, 删除节点操作,可以用新建节点代替 维护每个点到跟节点的距离 Gears Time Limit: 2 Seconds       Memory Limit: 65536 KB Bob has  ...

  9. ZOJ 2833 Friendship

    ZOJ 2833 Friendship(并查集) 题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827366332 Fr ...

最新文章

  1. Centos下机器学习算法Mahout库的安装和示例
  2. Javascript - Vue - webpack
  3. 航天总线测试系统软件,总线仿真测试平台软件
  4. Cocos2dx学习笔记(1) Ref类型数据 垃圾回收机制
  5. kalman filter卡尔曼滤波器- 数学推导和原理理解-----网上讲的比较好的kalman filter和整理、将预测值和观测值融和...
  6. Linux下SHELL的PS1变量简介
  7. STM32工作笔记0097---OEM厂是什么意思
  8. Deep learning 学习开篇
  9. 如何查找MySQL中查询慢的SQL语句
  10. 安卓recovery流程分析【第一篇】
  11. 怎么轻松学JAVA(三个月拿实习Offer):小猿的JAVA后端之路(持续更新)
  12. BugKu 旋转跳跃(mp3stego(mp3隐写工具)的使用)
  13. AUtoCAD Civil 3D-曲面-原始数据处理
  14. java jdbc 批处理_JDBC的批处理操作
  15. k8s中的端口hostPort、port、nodePort、targetPort
  16. python绘制capm模型
  17. linux服务器硬件配置,linux服务器硬件配置要求是多少
  18. [SIGMOD 2021] SharPer: Sharding Permissioned Blockchains Over Network Clusters
  19. 排列显示阿拉伯语、数字及英文时的处理方法
  20. Spring学习笔记(一):眼见为实,先上一个简单例子

热门文章

  1. linux命令详解:pgrep命令
  2. 表格和列表去虚线框的几种方式——Qt
  3. layui数据表格url是什么
  4. 【前端】主流浏览器,URL,URI
  5. Unity3D面试题整合
  6. 【实用技巧】输入法那些你不知道的秘密
  7. 2021-09-12-uu1
  8. 人参怎么吃最简单效果又好?
  9. 【Luat-air551G】2 熟悉NaviTrack查看信息
  10. 5.Excel vba开发-插入多行、多列,删除空行