ZOJ-4029-Now Loading!!! (2018 Zhejiang 15th Provincial Competition) (Thinking + Enumeration Optimization + Two Points)

https://zoj.pintia.cn/problem-sets/91827364500/problems/91827370263


 

DreamGrid has  integers . DreamGrid also has  queries, and each time he would like to know the value of

for a given number , where , .

Input

There are multiple test cases. The first line of input is an integer  indicating the number of test cases. For each test case:

The first line contains two integers  and  () -- the number of integers and the number of queries.

The second line contains  integers  ().

The third line contains  integers  ().

It is guaranteed that neither the sum of all  nor the sum of all  exceeds .

Output

For each test case, output an integer , where  is the answer for the -th query.

Sample Input

2
3 2
100 1000 10000
100 10
4 5
2323 223 12312 3
1232 324 2 3 5

Sample Output

11366
45619

 

The initial idea is to directly calculate the formula for each inquiry. This time complexity is O(n*m). Obviously, it is overtime for the data.

Now we consider how to optimize.

It can be seen that the power or log can be converted to the direction of enumeration if it appears. Because the value of this part is relatively small. (Compare Routines)

(For example, when p = 2, ai = between [2^30+1,2^31-1]), the maximum denominator is only 31

Considering that the denominator in the formula is so small, the problem should start from here

We can preprocess the prefix sum of the A array divided by the denominator.

Then when querying, see how many a[i] is in between 1~p^1, how many a[i] is in between p^1+1~p^2, and p^2+1~p^ How many a[i] between 3 is in, this part can be divided into two. Then prefix and statistics.

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=5e5+100;
typedef long long LL;
const LL mod=1e9;
LL a[maxn],sum[40][maxn];
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL t;cin>>t;
  while(t--)
  {
  	LL n,m;cin>>n>>m;
  	for(LL i=1;i<=n;i++) cin>>a[i];
  	sort(a+1,a+1+n); 
  	for(LL i=1;i<=32;i++){
  		sum[i][0]=0;
  		for(LL j=1;j<=n;j++)
		  {
		  	  sum[i][j]=(sum[i][j-1]+a[j]/i)%mod;
		  }	
	}
	LL ans=0;
	for(LL j=1;j<=m;j++)
	{
		LL p;cin>>p;
		LL temp=0;
		LL pos=1;
		for(LL i=1;i<=32;i++)
		{
			LL l=upper_bound(a+1,a+1+n,pos)-a;//a[i]在1~p范围内有多少,在p+1~p^2范围内有多少,在p^2+1~p^3范围内有多少.... 
			if(l>n) break;
			LL r=upper_bound(a+1,a+1+n,pos*p)-a;
			if(l==n+1) continue;
			temp=(temp+(sum[i][r-1]-sum[i][l-1]))%mod;
			temp=(temp+mod)%mod;
			pos*=p;
		}
		ans=(ans+temp*j%mod)%mod;
	}
	cout<<ans<<endl;
  }
return 0;
}

 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/108819199