Now Loading!!!(ZOJ Problem Set - 4029)

The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple 

F 题

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

题解:
首先将数组从小到大排序,因为分母很少,最大为30,提前把a[i]/分母 求前缀和,枚举分母,然后二分
若分母为i,二分pow(p,i-1) 和 pow(p,i) 的位置 根据位置把属于分母为 i 的区域求出来,若 pow(p,i-1) 超过 max(a[i]) 就break即可
pow(p,i-1) 不用快速幂 , 从 1 一直乘 即可。

时间复杂度为:O(n*30*log(n))

额 ,,,, 没想到mod是1e9
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>

using namespace std;
typedef long long ll;
const int N=5*1e5+7;
ll a[N],pre[32][N];
const int mod=1e9;

int main()
{
    int t,n,m;
    ll maxx;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        a[0]=0;
        sort(a+1,a+n+1);
        for(int i=1;i<=31;i++)
        {
            pre[i][0]=0;
            for(int j=1;j<=n;j++)
                pre[i][j]=(mod+pre[i][j-1]+(a[j]/i))%mod;
        }
        ll ans=0,p;
        maxx=a[n];
        for(int i=1;i<=m;i++)
        {
            scanf("%lld",&p);
            ll preNum=1,nowNum;
            ll res=0;
            for(int j=1;j<=31;j++)
            {
                int nowInd,preInd;
                if(preNum+1>maxx)  break;
                nowNum=preNum*p;
                if(nowNum+1<=maxx)
                    nowInd=lower_bound(a,a+n+1,nowNum+1)-a;
                else
                    nowInd=n;
                preInd=lower_bound(a,a+n+1,preNum+1)-a;
                if(a[nowInd]>=nowNum+1)
                    nowInd--;
               // cout<<nowInd<<" "<<nowNum<<endl;
              //  cout<<preInd<<" "<<preNum<<endl;
                if(nowInd>=preInd&&preInd>=1)
                {
                    res=(res+mod+pre[j][nowInd]-pre[j][preInd-1])%mod;
                  //  cout<<j<<"*"<<(pre[nowInd]-pre[preInd-1])/j<<endl;
                  //  cout<<preInd-1<<"-->"<<nowInd<<endl;
                }
                preNum=nowNum;
            }
            ans=(ans+res*i%mod)%mod;
          //  printf("$%lld\n",ans);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

posted @ 2018-04-30 18:54  jadelemon  阅读(464)  评论(0编辑  收藏  举报