2018ACM浙江省賽 ZOJ 4029 Now Loading!!!(二分)
阿新 • • 發佈:2019-01-31
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 45619Author: LIN, Xi
Source: The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple
【題意】
對於每一次詢問,求上式的值
【分析】
把a排序
由於分母的範圍很小 [2,30],可以列舉;
對於每一次詢問p,列舉分母 i 時,可以找出a中分母等於 i 的那一段,預處理字首和,用於此時直接加。
複雜度n * log * log
【程式碼】
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+5;
const int INF=0x3f3f3f3f;
const int mod=1e9;
int n,m;
int a[500010],p;
int sum[32][500010];
ll qpow(ll n,ll m)
{
ll ans=1;
while(m){
if(m&1)ans*=n;
n*=n;
m>>=1;
if(ans>20001001000)return -1;
}
return ans;
}
int main()
{
int T;cin>>T;
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
sort(a+1,a+1+n);
for(int i=1;i<=31;i++) //pre sum of a/log
{
sum[i][0]=0;
for(int j=1;j<=n;j++)
sum[i][j]=(sum[i][j-1]+a[j]/i)%mod;
}
ll ans=0;
for(int k=1;k<=m;k++)
{
scanf("%d",&p);
ll res=0;
for(int i=1;i<=31;i++) //log p ai
{
ll down=qpow(p,i-1); //> it
ll up=qpow(p,i); // <=it
if(down==-1)break;
int l=1,r=n+1,mid;
if(down!=-1)
while(l<r){
mid=(l+r)>>1;
if(a[mid]>down)r=mid;
else l=mid+1;
}
int L=r;
l=1,r=n+1;
if(up!=-1)
while(l<r){
mid=(l+r)>>1;
if(a[mid]>up)r=mid;
else l=mid+1;
}
int R=r-1;
if(L>n)break;
if(L>R)continue;
res=(res+sum[i][R]-sum[i][L-1])%mod;
res=(res+mod)%mod;
}
ans=(ans+res*k%mod)%mod;
}
printf("%lld\n",ans);
}
}