Now Loading!!! Time Limit: 1 Second Memory Limit: 131072 KB 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 45619 Author: LIN, Xi Source: The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple Submit Status
This question prefix sum + two points
We find that the denominator is only 1-30;
We construct a sum/i(i-30)
Binary search a^(i -1)<p<a^(i) The denominator is i
You divide a[i] into 30 segments.
Use flag to save the seat.
Each segment is k[j][flag[j]]-k[j][flag[j-1]]
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int maxn=500005; const int mod=1e9; int a[maxn],p[maxn],k[35][maxn]; int n,m; int main() { int t,flag[maxn],cnt; scanf("%d",&t); while(t--) { scanf("%d %d",&n,&m); for(int i=1; i<=n; i++) scanf("%d",&a[i]); sort(a+1,a+n+1); for(int i=1; i<=m; i++) scanf("%d",&p[i]); for(int i=1; i<=30; i++) { k[i][0]=0; for(int j=1; j<=n; j++) { k[i][j]=(k[i][j-1]+(a[j]/i))%mod; } } ll sum=0,ans; ll temp; for(int i=1; i<=m; i++) { temp=1; cnt=0; ans=0; while(temp*p[i]<=a[n]) { temp*=p[i]; int l=1,r=n; while(l<=r) { int mid=(l+r)/2; if(a[mid]<=temp) l=mid+1; else r=mid-1; } flag[++cnt]=r; } if(flag[cnt]<n) flag[++cnt]=n; for(int j=1; j<=cnt; j++) { ans = (ans+(k[j][flag[j]]-k[j][flag[j- 1 ]]))% mod; } sum=(sum+ans*i)%mod; } printf("%lld\n",(sum+mod)%mod); } return 0; }