题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4029
Time Limit: 1 Second Memory Limit: 131072 KB
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=5e5+;
const int maxm=5e5+;
const LL MOD=1e9; int n,m;
LL a[maxn],p[maxm];
int sum[maxn][]; void init()
{
sort(a+,a+n+);
for(int t=;t<=;t++)
{
sum[][t]=;
for(int i=;i<=n;i++) sum[i][t]=(sum[i-][t]+a[i]/t)%MOD;
}
} int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=;i<=n;i++) cin>>a[i];
for(int i=;i<=m;i++) cin>>p[i]; init(); LL ans=;
for(int i=;i<=m;i++)
{
LL z=;
int j=,k;
for(int t=;;t++)
{
if(round(pow(p[i],t)) < a[]) continue;
if(round(pow(p[i],t-)) >= a[n]) break; k=upper_bound(a+,a+n+,round(pow(p[i],t)))-(a+);
z=(z+sum[k][t]-sum[j][t])%MOD;
j=k;
}
ans=(ans+z*i)%MOD;
}
cout<<(ans+MOD)%MOD<<endl;
}
}