【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

相关文章

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3

找不到Anaconda prompt终端的原因分析及解决方案

《找不到Anacondaprompt终端的原因分析及解决方案》因为anaconda还没有初始化,在安装anaconda的过程中,有一行是否要添加anaconda到菜单目录中,由于没有勾选,导致没有菜... 目录问题原因问http://www.chinasem.cn题解决安装了 Anaconda 却找不到 An

Spring定时任务只执行一次的原因分析与解决方案

《Spring定时任务只执行一次的原因分析与解决方案》在使用Spring的@Scheduled定时任务时,你是否遇到过任务只执行一次,后续不再触发的情况?这种情况可能由多种原因导致,如未启用调度、线程... 目录1. 问题背景2. Spring定时任务的基本用法3. 为什么定时任务只执行一次?3.1 未启用

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

C++ 各种map特点对比分析

《C++各种map特点对比分析》文章比较了C++中不同类型的map(如std::map,std::unordered_map,std::multimap,std::unordered_multima... 目录特点比较C++ 示例代码 ​​​​​​代码解释特点比较1. std::map底层实现:基于红黑

Spring、Spring Boot、Spring Cloud 的区别与联系分析

《Spring、SpringBoot、SpringCloud的区别与联系分析》Spring、SpringBoot和SpringCloud是Java开发中常用的框架,分别针对企业级应用开发、快速开... 目录1. Spring 框架2. Spring Boot3. Spring Cloud总结1. Sprin

Spring 中 BeanFactoryPostProcessor 的作用和示例源码分析

《Spring中BeanFactoryPostProcessor的作用和示例源码分析》Spring的BeanFactoryPostProcessor是容器初始化的扩展接口,允许在Bean实例化前... 目录一、概览1. 核心定位2. 核心功能详解3. 关键特性二、Spring 内置的 BeanFactory

MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析

《MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析》本文将详细讲解MyBatis-Plus中的lambdaUpdate用法,并提供丰富的案例来帮助读者更好地理解和应... 目录深入探索MyBATis-Plus中Service接口的lambdaUpdate用法及示例案例背景