2018ACM浙江省赛 ZOJ 4029 Now Loading!!!(二分)

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

【题意】

对于每一次询问,求上式的值

【分析】

把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);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪的期许

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值