Now Loading!!! ZOJ - 4029 (二分+前缀和+思维)

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 .

<h4< dd="">Output

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

<h4< dd="">Sample Input
2
3 2
100 1000 10000
100 10
4 5
2323 223 12312 3
1232 324 2 3 5
<h4< dd="">Sample Output
11366
45619
题意:输入a,b数组,已知每一个z为
1inailogpai∑1≤i≤n⌊ai⌈logp⁡ai⌉⌋
,求
(i=1mizi)mod109

题意:就是那个式子,分母向上取整,分子向下取整,最后得出来一个m个数,再用下面的公式求和;

思路:经过观察,可以方法发现分母最大不会超过31,所有先预处理,你让 a数组对这 31 个数进行有前缀和,再接着,枚举b数组中的数的 31 次方,再用二分再 a 数组中找,利用前缀和 成段相加;

这道题:刚开始 Segmentation Fault,段错误,我在网上查,所谓的段错误就是指访问的内存超过了系统所给这个程序的

内存空间等,我把数组改小了点,删了两个没有数组,终于过了

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#include<math.h>

#define Max 500005
#define ll long long 
#define mod 1000000000

ll sum[Max][32];  //sum[i][j] 表示数组a(从小到大排过序的),数组a,位置i前面(包括i)每个数除以j的前缀和;  
ll a[Max];      
ll n,m;

void init()
{
	ll i;
	for(ll j = 1;j<=31;j++)
	{
		ll s = 0;
		for(i = 0;i<n;i++)
		{
			s += a[i]/j;
			sum[i+1][j] = s;
		}
	}
		
}

int main()
{
	ll t;
	scanf("%lld",&t);
	while(t--)
	{
		scanf("%lld%lld",&n,&m);
		ll ma = -1;
		for(ll i = 0;i<n;i++)
		{
			scanf("%lld",&a[i]);
			ma = max(ma,a[i]);
		}
		
		a[n] = mod+10;
		sort(a,a+n);
		init();
		ll res = 0;
		for(ll i = 0;i<m;i++)
		{
			ll temp,s,x;
			ll sum1 = 0,k = 1;
			ll flag = 0;
			scanf("%lld",&x);
			for(ll j = 1;j<=32;j++)
			{
				if(flag) break;
				if(j>1) temp = s;
				k = k * x;
				if(k>ma&&!flag)
				{
					flag = 1;
					s = n;	
				}
				else s = upper_bound(a,a+(n+1),k) - a;  
				if(j>1)
				{
					sum1 += (sum[s][j] - sum[temp][j]);  // 成段累加; 
					sum1 %=mod;
				}
				else 
				{
					sum1 += sum[s][j];
					sum1 %=mod;
				}
		
			}
			res = (res + (sum1*(i+1))%mod)%mod;
		}
		printf("%lld\n",res);
	}
	return 0;	
}

猜你喜欢

转载自blog.csdn.net/obsorb_knowledge/article/details/80318487
now