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

思路

对于a[i]的范围为[2,1e9],我们可以知道log p (a[i])的值一定小于30,因此,我们可以把输入的a[ ]先预处理一下,把floor(a[i]/k)(k属于[1,30])的值的前缀和先存下来(注意:这里要用vector,ll sum[30][5e5+10]的数组开不下,会导致段错误)。


接下来,我们要确定对于当前p,log p (a[i])的值具体为多少:

易得,当a[i]<=p的时候,log p (a[i])向上取整的值为1;

当p<a[i]<=p^2的时候,log p (a[i])向上取整的值为2;

当p^2<a[i]<=p^3的时候,log p (a[i])向上取整的值为3;

......

因此,我们可以把p,p^2,p^3,...,p^n存下来,只要存到p^n>1e9即可。


得到这两个vector之后,我们就可以通过遍历p[i]来计算答案了:

对于一个p,我们先在a[i]中找到<=p的下标位置,ans+=这个位置之前的a[i]除以1后的前缀和;再在a[i]找到<=p^2的下标位置,ans+=这个位置之前的a[i]除以2后的前缀和;...;知道p^n的值大于a[i]中的所有元素就结束循环。


至于如何找,当然是二分了,注意要先把a[i]排个序。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

typedef long long ll;

const int MAX = 5e5 + 10;
const ll MOD = 1e9;

ll a[MAX], p[MAX];
vector<ll>sum[40];
vector<ll>mu[MAX];

int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		int n, m;
		scanf("%d%d", &n, &m);
		for (int i = 1; i <= n; i++)
			scanf("%lld", &a[i]);
		sort(a + 1, a + n + 1);
		for (int i = 0; i < m; i++)
			scanf("%lld", &p[i]);
		for (int i = 0; i <= 35; i++)
			sum[i].clear();
		for (int i = 1; i <= 32; i++)
		{
			sum[i].push_back(0);
		}
		for (int i = 1; i <= 32; i++)
		{
			sum[i].push_back(a[1] / i);
		}
		for (int i = 2; i <= n; i++)
		{
			for (int j = 1; j <= 32; j++)
			{
				ll temp = sum[j][i - 1] + a[i] / j;
				sum[j].push_back(temp);
			}
		}
		for (int i = 0; i <= m; i++)
			mu[i].clear();
		for (int i = 0; i < m; i++)
		{
			ll res = p[i];
			mu[i].push_back(p[i]);
			while (true)
			{
				res = res*p[i];
				mu[i].push_back(res);
				if (res > (1e9 + 100))
					break;
			}
		}
		ll ans = 0;
		for (int i = 0; i < m; i++)
		{
			ll tmp = 0;
			int pre = 0;
			for (int j = 0; j < mu[i].size(); j++)
			{
				int pos = upper_bound(a + 1, a + n + 1, mu[i][j]) - a;
				pos = pos - 1;
				if (pos == 0)  continue;
				tmp = (tmp + sum[j + 1][pos] - sum[j + 1][pre]) % MOD;
				if (pos == n)  break;
				pre = pos;
			}
			ans = (ans + tmp*(i + 1) % MOD) % MOD;
		}
		printf("%lld\n", ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值