浙江省赛 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

分母范围很小,暴力分母存一个前缀和

每次加上就行了

转自点击打开链接

 

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <utility>
#include <set>
#include <bitset>
#include <vector>
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fLL
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
int min3(int a,int b,int c){return min(min(a,b),c);}
int max3(int a,int b,int c){return max(max(a,b),c);}
int gcd(int x, int y){if(y==0)return x;return gcd(y, x%y);}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();return x*f;}

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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值