F - 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

利用前缀数组求分母相同的数的和,用二分函数upper_bound求对应分母相同的序列的区间,但先要把序列从小到大排序,为什么不用lower_bound是因为,比如p为10,有个数为101,那么它对应的分母为3,不是2,不应该和分母为2的一起计数
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MOD = 1e9;
int t, n, m;
ll a[100015], sum[100015][35];
int main() {
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; ++i) {
            scanf("%lld", &a[i]);
        }
        sort(a + 1, a + n + 1);
        for (int k = 1; k <= 30; ++k) {
            for (int i = 1; i <= n; ++i) {
                sum[i][k] = sum[i - 1][k] + a[i] / k;
            }
        }
        ll res = 0;
        for (int rep = 1; rep <= m; ++rep) {
            ll ans = 0, p;
            scanf("%lld", &p);
            ll tt = p;
            ll tt2 = 1;
            int j = 1;
           while(true){
                int pos = upper_bound(a+1, a+1+n, tt) - (a+1);//get_pos(i, tt);
                int pos2 = upper_bound(a+1, a+1+n, tt2) - (a+1);
                ans += sum[pos][j] - sum[pos2][j];
                ++j;
                tt2 = tt;
                tt *= p;
                if(pos == n) break;
                //printf("pos = %d, tt = %lld, i = %d\n", pos, tt, i);
            }
            res = (res + ans * rep) % MOD;
        }
        printf("%lld\n", res);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值