ZOJ-4029-Now Loading!!!(2018浙江第15届省赛)-二分

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

题意:给出数组A,和m次询问,计算以上公式

题解:初始的想法是对于每次询问,直接去算公式,这样的时间复杂度是O(n*m)对于题给数据显然是超时的,

现在我们考虑怎么优化,可以想到的是每次查询时,都会重复除以同一个数。为什么这么说呢,因为[logp(ai)]最大为31

(当p = 2,ai = 在[2^30+1,2^31-1]之间时)

考虑到公式中的分母如此之小,题目应该从这里入手

我们可以预先处理A数组的前缀和,注意应该是处理A[]除以分母的前缀和,我们不能在查询的时候计算出区间的值然后再除,这样不符合公式的定义。

枚举分母然后就是二分查找上下界即可。

temp = (temp + sum[i][r-1] - sum[i][l-1]) % mod;
temp有可能减到负数
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod  = 1e9;
const ll INF = (ll)1<<33;
const int maxn = 5e5+10;
ll arr[maxn],sum[32][maxn];
ll n,m;
namespace IO
{
    template <typename T>
    inline bool scan_d (T &ret)
    {
        char c;
        int sgn;
        if (c = getchar(), c == EOF)return false; //EOF
        while (c != '-' && (c < '0' || c > '9') )
            if((c = getchar()) == EOF) return false;
        sgn = (c == '-') ? -1 : 1;
        ret = (c == '-') ? 0 : (c - '0');
        while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
        ret *= sgn;
        return true;
    }

    template<typename T>
    void print(T x)
    {
        static char s[33], *s1;
        s1 = s;
        if (!x) *s1++ = '0';
        if (x < 0) putchar('-'), x = -x;
        while(x) *s1++ = (x % 10 + '0'), x /= 10;
        while(s1-- != s) putchar(*s1);
    }

    template<typename T>
    void println(T x)
    {
        print(x);
        putchar('\n');
    }
};
int main()
{
    int caset;IO::scan_d(caset);
    while(caset--) {
        IO::scan_d(n);IO::scan_d(m);
        for(int i=1;i<=n;i++) IO::scan_d(arr[i]);
        sort(arr+1,arr+1+n);

        for(int i=1;i<=31;i++) {
            sum[i][0] = 0;
            for(int j=1;j<=n;j++) {
                sum[i][j] = (sum[i][j-1] + arr[j] / i) % mod;
            }
        }

        ll ans = 0,p;
        for(ll j=1;j<=m;j++) {
            ll temp = 0,l,r;
            IO::scan_d(p);
            ll pos = 1;
            for(ll i=1;i<=31;i++) {
                l = upper_bound(arr+1,arr+n+1,pos) - arr;
                if(l > n) break;
                r = upper_bound(arr+1,arr+n+1,pos*p) - arr;
                ///if(l == n) continue;
                if(l == n+1) continue;
                ///temp = (temp + ((sum[r-1] - sum[l-1])/i)) % mod;
                temp = (temp + sum[i][r-1] - sum[i][l-1]) % mod;
                temp = (temp + mod) % mod; 
                pos *= p;
            }
            ans = (ans + temp * j % mod) % mod;
        }
        IO::println(ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值