fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b) {
  14. int x = 1;
  15. a %= M;
  16. while (b) {
  17. if (b & 1) x = (x * a) % M;
  18. a = (a * a) % M;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27. vector<int> a;
  28.  
  29. int modInverse(int n){
  30. return power(n, M-2);
  31. }
  32.  
  33. int consistency(int n, int m){
  34.  
  35. unordered_map<int,int> freq;
  36. vector<int> b;
  37.  
  38. for(int i=0; i<n; i++){
  39. if(!freq.count(a[i])){
  40. b.push_back(a[i]);
  41. }
  42. freq[a[i]]++;
  43. }
  44.  
  45. sort(begin(b), end(b));
  46. n = b.size();
  47.  
  48.  
  49. int freqM = 1;
  50. int ans = 0;
  51.  
  52. int s = 0, e = 0;
  53. while(e<n){
  54.  
  55. freqM = (freqM * freq[b[e]])%M;
  56.  
  57. if(e-s+1 < m){
  58. e++;
  59. }
  60. else{
  61.  
  62. while(s<=e && b[e]-b[s] >= m){
  63. freqM = (freqM * modInverse(freq[b[s]])) % M;
  64. s++;
  65. }
  66.  
  67. if(e-s+1 == m){
  68. ans = (ans + freqM)%M;
  69. }
  70. e++;
  71. }
  72. }
  73.  
  74.  
  75. return ans;
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. int practice(int n, int m){
  93.  
  94.  
  95. return 0;
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102. void solve() {
  103.  
  104. int n, m;
  105. cin>> n >> m;
  106.  
  107. a.resize(n);
  108. for(int i=0; i<n; i++) cin >> a[i];
  109.  
  110. cout << consistency(n, m) << endl;
  111.  
  112.  
  113. }
  114.  
  115.  
  116.  
  117.  
  118.  
  119. int32_t main() {
  120. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  121.  
  122. int t = 1;
  123. cin >> t;
  124. while (t--) {
  125. solve();
  126. }
  127.  
  128. return 0;
  129. }
Success #stdin #stdout 0s 5312KB
stdin
9
7 4
8 10 10 9 6 11 7
5 3
4 2 2 3 6
8 2
1 5 2 2 3 1 3 3
3 3
3 3 3
5 1
3 4 3 10 7
12 3
5 2 1 1 4 3 5 5 5 2 7 5
1 1
1
3 2
1 2 3
2 2
1 2
stdout
5
2
10
0
5
11
1
2
1