Zoj 4029 Now Loading!!! (二分)

Now Loading!!!

Time Limit: 1 Second       Memory Limit: 131072 KB

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

枚举底数 二分合法分母;

坑点:不必要的数组不开,容易mle,对于每个分母的合法情况,用二维前缀和预处理一下,注意向下取整向上取整的情况

AC代码

#include <iostream>
#include <queue>
#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+20;
const ll lim=1e18+10;
const int mod=1e9;
const int inf=1e9+10;
ll a[maxn];
ll sto[32][maxn],p;
int n;
ll mul(ll a,ll b)
{
	if (a>lim/b)
		return lim;
	a=a*b;
	if (a>lim)
		return lim;
	else
		return a;
}
int s_s(ll v)
{
	int l=1,r=n,po,mid;
	while (l<r) //找大于等于某个数的第一个数, 
	{
		mid=(l+r)>>1;
		if (a[mid]>=v)
			r=mid;
		else
		l=mid+1;
	}
	return r;
}
int s_r(ll v) //找小于等于v的最后一个数;如果不存在,返回0; 
{
	int l=1,r=n,po=0;
	while (l<=r)
	{
		int mid=(l+r)>>1;
		if (a[mid]<=v)
		{
			po=mid;
			l=mid+1;
		}
		else
			r=mid-1;
	}
	return po;
}
int main()
{
	int v,m,t;
	scanf("%d",&t);
	while (t--)
	{
		ll Sum=0;
		scanf("%d%d",&n,&m);
		for (int i=1;i<=n;i++)
			scanf("%lld",&a[i]);
			sort(a+1,a+1+n);
		for (int i=1;i<=31;i++)
			sto[i][0]=0;
		for (int i=1;i<=31;i++)
			for (int j=1;j<=n;j++)
				sto[i][j]=(sto[i][j-1]+a[j]/i)%mod;
	    ll vv,vv1;
		for (int i=1;i<=m;i++)
		{ 
			vv=1;
			scanf("%lld",&p);
			for (int j=1;j<=31;j++)
			{
				vv1=mul(vv,p);
				int po1=s_s(vv+1);
				int po2=s_r(vv1);
				if (vv>inf)
				break;
				if (po1==n&&a[po1]<(vv+1)||po2==0)
				{
					vv=mul(vv,p);
					continue;
				}
				else if (a[po1]>=(vv+1)&&a[po2]<=vv1&&po2>=po1)
					Sum=(Sum+(ll)(i*(sto[j][po2]-sto[j][po1-1]+mod))%mod)%mod;
				vv=mul(vv,p);
			}
		}
		printf("%lld\n",Sum);
	}
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值