-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclosetuplehard.cpp
94 lines (73 loc) · 1.63 KB
/
closetuplehard.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll maxn = 2e5+5;
const ll mod = 1e9 + 7;
ll fact[maxn+10]{0};
ll invfact[maxn+10]{0};
ll binpow(ll x, ll n) {
assert(n >= 0);
x %= mod; //note: m*m must be less than 2^63 to avoid ll overflow
ll res = 1;
while (n > 0) {
if (n % 2 == 1) //if n is odd
res = res * x % mod;
x = x * x % mod;
n /= 2; //divide by two
}
return res;
}
ll modinv(ll x) {
return binpow(x, mod - 2);
}
void precompute() {
fact[0] = 1;
invfact[0] = 1;
for (ll i = 1; i < maxn; i++) {
fact[i] = (fact[i - 1] * i) % mod;
invfact[i] = modinv(fact[i]);
}
}
ll choose(ll n, ll k) {
if (n < k) return 0;
return (fact[n] * ((invfact[k] * invfact[n - k] % mod))) % mod;
}
bool ok(ll mn, ll mx, ll k) { return mx - mn <= k; }
ll solve() {
ll sum = 0;
ll n, m, k;
cin >> n >> m >> k;
ll a[maxn];
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
ll lo = 0;
for (ll mn = 0; mn < n; mn++) {
ll hi = n - 1;
// bin search for max
while (lo < hi) {
ll mx = (hi + lo + 1) / 2;
if (ok(a[mn], a[mx], k)) {
lo = mx;
} else {
hi = mx - 1;
}
}
// Calculate possible tuples in range (mn, mx)
ll p = lo - mn;
ll c = m - 1;
sum += choose(p, c);
sum %= mod;
}
return sum;
}
int main() {
ll t;
cin >> t;
precompute();
while (t--) {
cout << solve() << endl;
}
return 0;
}