-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB.cpp
75 lines (75 loc) · 1.68 KB
/
B.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1.5e6;
const int mod = 1000000000 + 7;
int a[maxn];
int x[maxn], y[maxn];
int dif[maxn];
long long power(int b, int e) {
long long pw = b;
long long ans = 1;
while(e) {
if(e & 1) ans = (ans * pw) % mod;
pw = (pw * pw) % mod;
e >>= 1;
}
return ans;
}
void solve() {
int n, p;
scanf("%d %d", &n, &p);
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a, a + n, greater <int> ());
if(p == 1) {
printf("%d\n", n % 2);
return ;
}
long long sumA = 0, sumB = 0;
set <int> P, Q, rel;
for(int i = 0; i < n; i++) {
int e = P.empty() ? -1 : *P.rbegin();
int f = Q.empty() ? -1 : *Q.rbegin();
int cur = a[i];
while(true) {
if(e < f) {
if(x[cur] < p - 1) break;
x[cur++] = 0;
} else {
if(y[cur] < p - 1) break;
y[cur++] = 0;
}
}
if(e < f) {
sumA += power(p, a[i]);
sumA %= mod;
x[cur] += 1;
}
else {
sumB += power(p, a[i]);
sumB %= mod;
y[cur] += 1;
}
for(int j = a[i]; j <= cur; j++) {
if(dif[j] < 0) Q.erase(j);
else if (dif[j] > 0) P.erase(j);
dif[j] = x[j] - y[j];
if(dif[j] < 0) Q.insert(j);
else if (dif[j] > 0) P.insert(j);
rel.insert(j);
}
}
for(auto i : rel) {
x[i] = y[i] = dif[i] = 0;
}
int e = P.empty() ? -1 : *P.rbegin();
int f = Q.empty() ? -1 : *Q.rbegin();
if(e < f) printf("%lld\n", (mod + sumB - sumA) % mod);
else printf("%lld\n", (mod + sumA - sumB) % mod);
}
int main() {
int t;
scanf("%d", &t);
while(t--) solve();
}