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.

题目链接: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+二分)的更多相关文章

  1. ZOJ 3940 Modulo Query

    0--M对某个数字取模,相当于把0--M区间进行切割,每次暴力切割一下.结果的算的时候二分一下即可... 看了官方题解才会... #include<cstdio> #include< ...

  2. ZOJ 3940 Modulo Query (2016年浙江省赛E题,区间折叠 + map运用)

    题目链接  2016 ZJCPC Problem E 考虑一个开区间$[0, x)$对$a_{i}$取模的过程. $[0, x)$中小于$a_{i}$的部分不变,大于等于$a_{i}$的部分被切下来变 ...

  3. 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 ...

  4. ZOJ 3911 Prime Query(线段树)

    Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a s ...

  5. 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$ ...

  6. 2018 青岛ICPC区域赛E ZOJ 4062 Plants vs. Zombie(二分答案)

    Plants vs. Zombies Time Limit: 2 Seconds      Memory Limit: 65536 KB BaoBao and DreamGrid are playin ...

  7. zoj 2362 Beloved Sons【二分匹配】

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2361 来源:http://acm.hust.edu.cn/vjudg ...

  8. ZOJ 5638——Prime Query——————【线段树区间更新,区间查询,单点更新】

    Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a s ...

  9. ZOJ 2112 Dynamic Rankings(二分,树套树)

    动态区间询问kth,单点修改. 区间用线段树分解,线段树上每条线段存一颗平衡树. 不能直接得到kth,但是利用val和比val小的个数之间的单调性,二分值.log^3N. 修改则是一次logN*log ...

随机推荐

  1. 【洛谷1337】[JSOI2004] 吊打XXX(模拟退火经典题)

    点此看题面 大致题意: 一个平面上有\(n\)个点,每个点有1个权值,现在要选择平面上的一个点,使这\(n\)个点的权值乘上到达选定点的距离之和最小. 模拟退火 我们可以用模拟退火来做这道题. 先将\ ...

  2. python request下载文件时,显示进度以及网速

    import requests import time def downloadFile(name, url): headers = {'Proxy-Connection':'keep-alive'} ...

  3. MAC 设置登录欢迎语

    MacdeMacBook-Pro:etc mac$ cd /etc MacdeMacBook-Pro:etc mac$ cat motd 技术沉淀,空杯心态! _______ _______ _ __ ...

  4. mysql主从复制及双主复制

    之前做过一次在单台机器上的多实例的mysql,这次分开做,使用两台主机. 这里使用的主机地址分别为: MASTER:192.168.214.135 SLAVE  : 192.168.214.128 这 ...

  5. 六、Shell echo命令

    Shell echo命令 Shell 的 echo 指令与 PHP 的 echo 指令类似,都是用于字符串的输出.命令格式: echo string 您可以使用echo实现更复杂的输出格式控制. 1. ...

  6. 3.Cisco Packet Tracer中关于交换机端口安全的设置

    本次实验将在这幅拓扑图的基础上完成 我们会对pc0在交换机上进行mac地址绑定,pc1访问时则交换机断开端口 1.为pc机配置ip地址 pc0:192.168.1.1 pc1:192.168.1.2 ...

  7. Session 会话保持

    本文将详细讨论session的工作机制并且对在Java web application中应用session机制时常见的问题作出解答 一.术语session session,中文经常翻译为会话,其本来的 ...

  8. Ubuntu samba 安装与配置 实现windows和虚拟机中的Ubuntu共享文件

    2.    安装sumba服务 sudo apt-get install samba samba-common 这里出现了小问题, Ubuntu上安装samba不能安装的问题,“下列的软件包有不能满足 ...

  9. 各种友(e)善(xin)数论总集(未完待续),从入门到绝望

    目录 快速幂 扩展欧几里得 GCD 扩展欧几里得 同余系列 同余方程 同余方程组 一点想法 高次同余方程 BSGS exBSGS 线性筛素数 埃式筛 欧拉筛 欧拉函数 讲解 两道水题 法雷级数 可见点 ...

  10. A1055 The World's Richest(25 分)

    A1055 The World's Richest(25 分) Forbes magazine publishes every year its list of billionaires based ...