15th 浙江省赛 F Now Loading!!!

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[i] / k的前缀和,查询时二分求出相同的一段区间。复杂度O(n*k+ nlogn),k小于32
 1 #include <iostream>
 2 #include <fstream>
 3 #include <sstream>
 4 #include <cstdlib>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <string>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <queue>
11 #include <stack>
12 #include <vector>
13 #include <set>
14 #include <map>
15 #include <list>
16 #include <iomanip>
17 #include <cctype>
18 #include <cassert>
19 #include <bitset>
20 #include <ctime>
21 
22 using namespace std;
23 
24 #define pau system("pause")
25 #define ll long long
26 #define pii pair<int, int>
27 #define pb push_back
28 #define mp make_pair
29 #define clr(a, x) memset(a, x, sizeof(a))
30 
31 const double pi = acos(-1.0);
32 const int INF = 0x3f3f3f3f;
33 const int MOD = 1e9;
34 const double EPS = 1e-9;
35 
36 /*
37 #include <ext/pb_ds/assoc_container.hpp>
38 #include <ext/pb_ds/tree_policy.hpp>
39 
40 using namespace __gnu_pbds;
41 tree<pli, null_type, greater<pli>, rb_tree_tag, tree_order_statistics_node_update> T;
42 */
43 
44 int t, n, m;
45 ll a[100015], sum[100015][30];
46 int get_pos(int sta, ll tt) {
47     int s = sta, e = n, mi, res = sta - 1;
48     while (s <= e) {
49         mi = s + e >> 1;
50         if (a[mi] <= tt) {
51             s = (res = mi) + 1;
52         } else {
53             e = mi - 1;
54         }
55     }
56     //printf("sta = %d, tt = %lld, res = %d\n", sta, tt, res);
57     return res;
58 }
59 int main() {
60     scanf("%d", &t);
61     while (t--) {
62         scanf("%d%d", &n, &m);
63         for (int i = 1; i <= n; ++i) {
64             scanf("%lld", &a[i]);
65         }
66         sort(a + 1, a + n + 1);
67         for (int k = 1; k <= 30; ++k) {
68             for (int i = 1; i <= n; ++i) {
69                 sum[i][k] = sum[i - 1][k] + a[i] / k;
70             }
71         }
72         ll res = 0;
73         for (int rep = 1; rep <= m; ++rep) {
74             ll ans = 0, p;
75             scanf("%lld", &p);
76             ll tt = p;
77             for (int i = 1, j = 1; i <= n; ) {
78                 int pos = get_pos(i, tt);
79                 ans += sum[pos][j] - sum[i - 1][j];
80                 ++j;
81                 tt *= p;
82                 i = pos + 1;
83                 //printf("pos = %d, tt = %lld, i = %d\n", pos, tt, i);
84             }
85             res = (res + ans % MOD * rep) % MOD;
86         }
87         printf("%lld\n", res);
88     }
89     return 0;
90 }
View Code

 

posted @ 2018-04-29 19:53  hit_yjl  阅读(420)  评论(0编辑  收藏  举报