浙江省赛 ZOJ4029
Posted 2014>
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浙江省赛 ZOJ4029相关的知识,希望对你有一定的参考价值。
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
这题前缀和+二分
我们发现分母只有1-30;
我们构造一个sum/i(i-30)
二分查找 a^(i -1)<p<a^(i) 分母 就是 i
你就把a[i]分成30段。
用flag 去保存位子。
每一段就是 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; }
以上是关于浙江省赛 ZOJ4029的主要内容,如果未能解决你的问题,请参考以下文章
2017浙江省赛 B - Problem Preparation ZOJ - 3959
2017浙江省赛 E - Seven Segment Display ZOJ - 3962
2017浙江省赛 H - Binary Tree Restoring ZOJ - 3965
2017浙江省赛 D - Let's Chat ZOJ - 3961