【ZOJ3940 The 13th Zhejiang Provincial Collegiate Programming ContestE】【脑洞 STL-MAP 复杂度分析 区间运算思想 双指针】M

本文主要是介绍【ZOJ3940 The 13th Zhejiang Provincial Collegiate Programming ContestE】【脑洞 STL-MAP 复杂度分析 区间运算思想 双指针】M,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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 ≤ iN.
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 + ... + QZQ) 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<stdio.h> 
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5 + 10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int casenum, casei;
int n, m, q;
map<int, int>mop;
map<int, int>::iterator it;
pair<int, int>a[N];
int main()
{scanf("%d", &casenum);for (casei = 1; casei <= casenum; ++casei){mop.clear();scanf("%d%d", &n, &m);mop[m + 1] = 1;for (int i = 1; i <= n; ++i){int x; scanf("%d", &x);while(1){it = mop.upper_bound(x);if (it == mop.end())break;mop[x] += it->first / x * it->second;if(it->first%x)mop[it->first%x] += it->second;mop.erase(it);}}int sum = 0;for (it = mop.begin(); it != mop.end(); ++it)sum += it->second;scanf("%d", &q);for (int i = 1; i <= q; ++i)scanf("%d", &a[i].first), a[i].second = i;sort(a + 1, a + q + 1);it = mop.begin();int ans = 0;for (int i = 1; i <= q; ++i){while (a[i].first >= it->first){sum -= it->second;++it;if (it == mop.end())break;}if (it == mop.end())break;ans = (ans + (LL)sum * a[i].second) % Z;}printf("%d\n", ans);}return 0;
}
/*
【题意】
有n(1e5)个数字a[](1e9),我们有q(1e5)个询问。
对于每个询问,想问你——有多少个[0,m](m∈[0,1e9])范围的数,满足其mod a[1] mod a[2] mod a[3] mod ... mod a[n]== b[i]
(b[]是1e9范围的数)【类型】
暴力
复杂度分析【分析】
这道题的关键之处,在于要想到——
取模不仅仅是一个数可以取模,一个区间我们也可以做取模处理。
进一步我们发现,取模得到的区间左界必然都为0
一个区间[0,r)的数 mod a[i],
如果r>a[i],那么——
这个区间会变成r/a[i]个[0,a[i])的区间,以及一个[0,r%a[i])的区间这样,我们对于每个a[i],我们就把所有>a[i]的区间都做处理。
在所有处理都完成之后,我们可以用双指针的方式处理所有询问的答案【时间复杂度&&优化】
O(nlognlogn)
这题的复杂度为什么是这样子呢?对于一个数,这个数做连续若干次的取模运算, 数值变化次数不会超过logn次。
于是我们以区间做取模运算,数值变化次数不会超过nlogn次。
然后加上map的复杂度,总的复杂度不过nlognlogn,可以无压力AC之。*/


这篇关于【ZOJ3940 The 13th Zhejiang Provincial Collegiate Programming ContestE】【脑洞 STL-MAP 复杂度分析 区间运算思想 双指针】M的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/610384

相关文章

Nginx分布式部署流程分析

《Nginx分布式部署流程分析》文章介绍Nginx在分布式部署中的反向代理和负载均衡作用,用于分发请求、减轻服务器压力及解决session共享问题,涵盖配置方法、策略及Java项目应用,并提及分布式事... 目录分布式部署NginxJava中的代理代理分为正向代理和反向代理正向代理反向代理Nginx应用场景

Redis中的有序集合zset从使用到原理分析

《Redis中的有序集合zset从使用到原理分析》Redis有序集合(zset)是字符串与分值的有序映射,通过跳跃表和哈希表结合实现高效有序性管理,适用于排行榜、延迟队列等场景,其时间复杂度低,内存占... 目录开篇:排行榜背后的秘密一、zset的基本使用1.1 常用命令1.2 Java客户端示例二、zse

Redis中的AOF原理及分析

《Redis中的AOF原理及分析》Redis的AOF通过记录所有写操作命令实现持久化,支持always/everysec/no三种同步策略,重写机制优化文件体积,与RDB结合可平衡数据安全与恢复效率... 目录开篇:从日记本到AOF一、AOF的基本执行流程1. 命令执行与记录2. AOF重写机制二、AOF的

MyBatis Plus大数据量查询慢原因分析及解决

《MyBatisPlus大数据量查询慢原因分析及解决》大数据量查询慢常因全表扫描、分页不当、索引缺失、内存占用高及ORM开销,优化措施包括分页查询、流式读取、SQL优化、批处理、多数据源、结果集二次... 目录大数据量查询慢的常见原因优化方案高级方案配置调优监控与诊断总结大数据量查询慢的常见原因MyBAT

分析 Java Stream 的 peek使用实践与副作用处理方案

《分析JavaStream的peek使用实践与副作用处理方案》StreamAPI的peek操作是中间操作,用于观察元素但不终止流,其副作用风险包括线程安全、顺序混乱及性能问题,合理使用场景有限... 目录一、peek 操作的本质:有状态的中间操作二、副作用的定义与风险场景1. 并行流下的线程安全问题2. 顺

MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决

《MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决》MyBatis默认开启一级缓存,同一事务中循环调用查询方法时会重复使用缓存数据,导致获取的序列主键值均为1,... 目录问题原因解决办法如果是存储过程总结问题myBATis有如下代码获取序列作为主键IdMappe

Rust 智能指针的使用详解

《Rust智能指针的使用详解》Rust智能指针是内存管理核心工具,本文就来详细的介绍一下Rust智能指针(Box、Rc、RefCell、Arc、Mutex、RwLock、Weak)的原理与使用场景,... 目录一、www.chinasem.cnRust 智能指针详解1、Box<T>:堆内存分配2、Rc<T>:

Java中最全最基础的IO流概述和简介案例分析

《Java中最全最基础的IO流概述和简介案例分析》JavaIO流用于程序与外部设备的数据交互,分为字节流(InputStream/OutputStream)和字符流(Reader/Writer),处理... 目录IO流简介IO是什么应用场景IO流的分类流的超类类型字节文件流应用简介核心API文件输出流应用文

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数