#include<bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 9;
int n, d;
vector<int>v;
int dp[20][(1 << 20)];
int rec(int indx, int mask){
int res = 0;
if(mask == 0)return 1;
if(dp[indx][mask] != -1)return dp[indx][mask];
for(int i = 0;i < n;++i){
if((mask & (1 << i)) && v[indx] + d >= v[i]){
res += rec(i, mask ^ (1 << i));
res %= MOD;
}
}
return dp[indx][mask] = res;
}
vector<vector<int>>G;
vector<bool>vis;
int cnt = 0;
int result = 0;
void dfs(int node){
vis[node] = 1;
cnt++;
if(cnt == n){
result++;
}
for(int &v : G[node]){
if(!vis[v]){
dfs(v);
}
}
vis[node] = 0;
cnt--;
}
int main()
{
// ios_base::sync_with_stdio(0);
// cin.tie(0);
cin >> n >> d;
v.resize(n);
for(int i = 0;i < n;++i){
cin >> v[i];
}
if(n <= 20){
int res = 0;
for(int i = 0;i < 20;++i){
for(int j = 0;j < (1 << 20);++j){
dp[i][j] = -1;
}
}
for(int i = 0;i < n;++i){
res += rec(i, ((1 << n) - 1) ^ (1 << i));
res %= MOD;
}
cout << res;
}
else{
G.resize(n + 1);
vis.resize(n + 1);
for(int i = 1;i <= n;++i){
for(int j = 1;j <= n;++j){
if(i == j)continue;
if(v[i - 1] + d >= v[j - 1])G[i].push_back(j);
}
}
for(int i = 1;i <= n;++i){
dfs(i);
}
cout << result;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
82244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
82264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
82268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
82268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
82484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
82528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
823 ms |
82488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
82268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
82268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1002 ms |
344 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1030 ms |
344 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1061 ms |
348 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1027 ms |
344 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1053 ms |
348 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1092 ms |
3672 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1027 ms |
12780 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
273 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
697 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
724 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
407 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |