#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
const int maxn = 100005;
const int inf = 1e9 + 5;
const int mod = 1e9 + 9;
int n, d;
int niz[maxn];
ll ans[maxn];
vector<pii> vec;
bool cmp(int s1, int s2){
return s1 > s2;
}
ll fak[maxn];
ll invz[maxn];
ll add(ll a, ll b){
return (a + b) % mod;
}
ll mul(ll a, ll b){
return (a * b) % mod;
}
ll power(ll a, ll b){
if(!b)return 1;
ll pola = power(a, b / 2);
pola = mul(pola, pola);
if(b % 2)pola = mul(pola, a);
return pola;
}
ll inv(ll a){
return power(a, mod - 2);
}
ll bin(int a, int b){
return mul(fak[a], mul(invz[b], invz[a - b]));
}
void init(){
fak[0] = 1;
ff(i,1,maxn - 1)fak[i] = mul(fak[i - 1], i);
invz[maxn - 1] = inv(fak[maxn - 1]);
fb(i,maxn - 2, 0)invz[i] = mul(invz[i + 1], i + 1);
}
int main()
{
ios::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
cin >> n >> d;
init();
ff(i,1,n)cin >> niz[i];
sort(niz + 1, niz + n + 1);
int j = 1;
ll br = 0;
ff(i,1,n){
br++;
while(niz[i] > niz[j] + d){
br--;
j++;
}
ans[i] = br;
}
ff(i,1,n){
if(ans[i] >= ans[i + 1])vec.pb({(ll)i, ans[i]});
}
// cout << endl;
// for(auto c : vec)cout << c.fi << " " << c.se << endl;
// cout << endl;
ll rez = fak[vec[0].se];
int len = (int)vec.size();
ff(i,1,len - 1){
ll x = vec[i].fi;
ll y = vec[i].se;
rez = mul(rez, fak[y]);
if(x - y + 1 <= vec[i - 1].fi){
ll presek = vec[i - 1].fi - (x - y + 1) + 1;
rez = mul(rez, invz[presek]);
}
}
cout << rez << endl;
return 0;
}
/**
7 4
1 2 3 4 5 6 7
4 1
1 2 3 100
6 9
10 20 20 10 10 20
// probati bojenje sahovski ili slicno
**/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
3696 KB |
Output is correct |
2 |
Correct |
13 ms |
3052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
19 ms |
5540 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
18 ms |
5612 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |