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 ...
随机推荐
- fread, fwrite - 二进制流的输入/输出
总览 (SYNOPSIS) #include <stdio.h> size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stre ...
- 2018.6.15 Java对象序列化详解
一.定义 Serializable 序列化:把Java对象转换为字节序列的过程. 反序列化:把字节序列恢复为Java对象的过程. ObjectOutputStream对象输出流 可以将实现了Seria ...
- 2017.12.6 计算机算法分析与设计---------Fibonacci数列
(1)题目: 无穷数列1,1,2,3,5,8,13,21,34,55,--,称为Fibonacci数列.它可以递归地定义为: 第n个Fibonacci数可递归地计算如下: int fibonacci( ...
- dn.net/blueheart20/article/details/22080489
dn.net/blueheart20/article/details/22080489
- 人品计算器 JFrame 窗体软件版 JPanel JTextField JTextArea JButtton JLabel setContentPane Swing包(用户界面工具包)
import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.eve ...
- 2.3.3 zerosum 和为零
#include<bits/stdc++.h> using namespace std; ],a; ]={' ','+','-'}; void out() { ;i<a;i++) c ...
- gravity 使用操作。
gravity 使用操作.最近我司有一个比较奇葩的需求,我们的环境是主从,因为数据量较大会定期的删除数据,最近不行了,要求新建出来一个库 同步正事环境的数据,但是要剔除 delete ,drop,tr ...
- phpstorm —— Xdebug 的配置和使用
给phpstorm 配置Xdebug(Xdebug 是 PHP 的一个扩展, 用于帮助调试和开发.它包含一个与 ide 一起使用的单步调试器.它升级了 PHP 的 var_dump () 功能) 这篇 ...
- 【Django】Django开发中的日志输出
开发环境:Ubuntu16.04+Django 1.11.9+Python2.7 一:使用自定义函数输出日志到log文件: import time def print_log(log): file_o ...
- JZOJ 5456. 【NOIP2017提高A组冲刺11.6】奇怪的队列
5456. [NOIP2017提高A组冲刺11.6]奇怪的队列 (File IO): input:queue.in output:queue.out Time Limits: 1000 ms Mem ...