Now Loading!!!
Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 262144/131072 K (Java/Others)
Problem Description
DreamGrid has n integers a1,a2,a3...an. DreamGrid also has m queries, and each time he would like to know the value of
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
【题目链接】 link
【题意】
RT
【思路】
由于n,m比较大,说明时间复杂度最多再乘以log。
这里由于log(a[i])最大只有30,那么我们考虑去预处理出a[i]/k的前缀和(k为分母)
然后每次对一个读入的p,令分母确定为tmp,那么a[i]的范围应该是[pow(p,tmp-1)+1,pow(p,tmp)],利用二分找出两个位置,然后用一下前缀和即可。
时间复杂度O(n*k+mloglog)
#include <cstdio>
#include <bits/stdc++.h>
#include <cmath>
#include <map>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
typedef long long ll;
const int maxn = 500005;
const ll mod = 1e9;
const int INF = 1e9;
const double eps = 1e-6;
int n,m;
ll a[maxn];
ll sum[maxn][32];
int main()
{
rush()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
sort(a+1,a+1+n);
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 ans=0;
for(int i=1;i<=m;i++)
{
ll x;
scanf("%lld",&x);
int k=1;
ll cnt=0;
int pos;
ll up=x;
for(int j=1;j<=n;j=pos+1)
{
pos=j-1;
int l=j,r=n;
while(l<=r)
{
int mid=(l+r)/2;
if(a[mid]<=up)
{
pos=mid;
l=mid+1;
}
else r=mid-1;
}
cnt+=sum[pos][k]-sum[j-1][k];
k++;
up*=x;
}
ans+=cnt%mod*(ll)i%mod;
ans%=mod;
}
printf("%lld\n",ans);
}
}