1. 程式人生 > >【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之。

*/