DreamGrid has integers . DreamGrid also has queries, and each time he would like to know the value of
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
利用前缀数组求分母相同的数的和,用二分函数upper_bound求对应分母相同的序列的区间,但先要把序列从小到大排序,为什么不用lower_bound是因为,比如p为10,有个数为101,那么它对应的分母为3,不是2,不应该和分母为2的一起计数
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9;
int t, n, m;
ll a[100015], sum[100015][35];
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%lld", &a[i]);
}
sort(a + 1, a + n + 1);
for (int k = 1; k <= 30; ++k) {
for (int i = 1; i <= n; ++i) {
sum[i][k] = sum[i - 1][k] + a[i] / k;
}
}
ll res = 0;
for (int rep = 1; rep <= m; ++rep) {
ll ans = 0, p;
scanf("%lld", &p);
ll tt = p;
ll tt2 = 1;
int j = 1;
while(true){
int pos = upper_bound(a+1, a+1+n, tt) - (a+1);//get_pos(i, tt);
int pos2 = upper_bound(a+1, a+1+n, tt2) - (a+1);
ans += sum[pos][j] - sum[pos2][j];
++j;
tt2 = tt;
tt *= p;
if(pos == n) break;
//printf("pos = %d, tt = %lld, i = %d\n", pos, tt, i);
}
res = (res + ans * rep) % MOD;
}
printf("%lld\n", res);
}
return 0;
}