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

``````#include<iostream>
#include<algorithm>
#include<cstdio>
typedef long long ll;
using namespace std;
int T,N,M;
const int MAXN = 5e5+10;
//存放 ai 的数组
int arr[MAXN];
//预先准备打表的数组
ll sum[32][MAXN];
//取余
const int mod = 1e9;
int main(){
scanf("%d",&T);

while(T--){
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++){
scanf("%d",&arr[i]);
}
//对其进行排序，为二分查找做准备
sort(arr+1,arr+N+1);
//打表，当分母为【1,31】这个区间的情况下，对 ai 求和，若要求某个区间的ai和,减去前缀项即可
for(int i=1;i<=31;i++){
sum[i][0] = 0;
//ai
for(int j=1;j<=N;j++){
sum[i][j] = (sum[i][j-1]+ arr[j]/i)%mod;
}
}

ll ans = 0;
//m次询问
for(int i=1;i<=M;i++){
int quer;
scanf("%d",&quer);
ll ans1 =0;
ll l = 1;
//分母最小的情况为1
ll mi =1;
ll x = quer;
while(l<=N){
//找到大于
while(x<arr[l]){
x *= quer;
mi++;
}
int r= upper_bound(arr+1,arr+1+N,x) - arr;
ans1 = (ans1 + (sum[mi][r-1] -sum[mi][l-1]+mod)%mod)%mod;
l = r;
}
ans = (ans +ans1*i)%mod;
}
printf("%lld\n",ans);
}

return 0;
}``````