ZOJ 4029

题目链接: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+10;
const int maxm=5e5+10;
const LL MOD=1e9;

int n,m;
LL a[maxn],p[maxm];
int sum[maxn][35];



void init()
{
    sort(a+1,a+n+1);
    for(int t=1;t<=30;t++)
    {
        sum[0][t]=0;
        for(int i=1;i<=n;i++) sum[i][t]=(sum[i-1][t]+a[i]/t)%MOD;
    }
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++) cin>>a[i];
        for(int i=1;i<=m;i++) cin>>p[i];

        init();

        LL ans=0;
        for(int i=1;i<=m;i++)
        {
            LL z=0;
            int j=0,k;
            for(int t=1;;t++)
            {
                if(round(pow(p[i],t)) < a[1]) continue;
                if(round(pow(p[i],t-1)) >= a[n]) break;

                k=upper_bound(a+1,a+n+1,round(pow(p[i],t)))-(a+1);
                z=(z+sum[k][t]-sum[j][t])%MOD;
                j=k;
            }
            ans=(ans+z*i)%MOD;
        }
        cout<<(ans+MOD)%MOD<<endl;
    }
}
原文地址:https://www.cnblogs.com/dilthey/p/9023472.html