-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloseTuples(HardVersion).cpp
More file actions
114 lines (102 loc) · 2.26 KB
/
Copy pathCloseTuples(HardVersion).cpp
File metadata and controls
114 lines (102 loc) · 2.26 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// https://codeforces.com/contest/1462/problem/E2
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int MAX_N = 2e5;
const int MOD = 1e9+7;
int fact[MAX_N+1], inv_fact[MAX_N+1];
int add(int a, int b) {
if (a+b >= MOD) {
return a+b-MOD;
}
return a+b;
}
int sub(int a, int b) {
if (b > a) {
return a-b+MOD;
}
return a-b;
}
int mult(int a, int b) {
return 1ll*a*b%MOD;
}
int pow(int n, int e) {
if (!e) {
return 1;
} else if (e&1) {
return mult(n, pow(n, e-1));
} else {
int srt = pow(n, e>>1);
return mult(srt, srt);
}
}
int inv(int n) {
// Fermat's little theorem for prime modulo.
return pow(n, MOD-2);
}
int nCr(int n, int r) {
if (n-r < 0) {
return 0;
}
return mult(fact[n], mult(inv_fact[r], inv_fact[n-r]));
}
// O(MAX_N).
void setUp() {
// Pre calculate factorials.
fact[0] = 1;
for (int i = 1; i <= MAX_N; ++i) {
fact[i] = mult(fact[i-1], i);
}
inv_fact[MAX_N] = inv(fact[MAX_N]);
for (int i = MAX_N-1; i >= 0; --i) {
inv_fact[i] = mult(inv_fact[i+1], i+1);
}
}
// Returns the maximum r such that a[r]-a[l] <= k.
int search(int l, vector<int>& a, int k) {
int low = l, mid, res = l, high = a.size()-1;
while (high >= low) {
mid = (low+high)>>1;
if (a[mid]-a[l] <= k) {
res = mid;
low = mid+1;
} else {
high = mid-1;
}
}
return res;
}
// O(n*log(n)).
void solveCase() {
int n, m, k;
cin >> n >> m >> k;
vector<int> a(n);
for (int& a_i : a) {
cin >> a_i;
}
sort(a.begin(), a.end());
int ans = 0;
for (int l = 0, r, last_r; l < n; ++l) {
r = search(l, a, k);
// Add up all possible combinations from [l,r].
ans = add(ans, nCr(r-l+1, m));
// Subtract out all previously added combinations.
if (l) {
ans = sub(ans, nCr(last_r-l+1, m));
}
last_r = r;
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
setUp();
int nC = 1;
cin >> nC;
for (int i = 1; i <= nC; i++) {
// cout << "\nTEST #" << i << ":\n";
solveCase();
}
return 0;
}