#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"
const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// -------------------------------------------------- Main Code --------------------------------------------------
int n, l;
int arr[16];
vector<vii> dp;
int sol(int mask, int prev, int sum) {
if (__builtin_popcount(mask) == n) return 1;
if (dp[mask][prev][sum] != -1) return dp[mask][prev][sum];
int ans = 0;
for (int i = 0; i < n; i++) {
if (mask&(1<<i)) continue;
int newsum = sum + (prev==n?0:abs(arr[prev]-arr[i]));
if (newsum <= l) ans = (ans + sol(mask|(1<<i), i, newsum))%MOD;
}
return dp[mask][prev][sum] = ans;
}
void sol() {
cin >> n >> l;
dp = vector<vii>((1<<n)+1, vii(n+1, vi(l+1, -1)));
for (int i = 0; i < n; i++) cin >> arr[i];
cout << sol(0, n, 0) << endl;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
sol();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
4 ms |
9052 KB |
Output is correct |
6 |
Correct |
3 ms |
8028 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
2 ms |
1116 KB |
Output is correct |
9 |
Correct |
5 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
1624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
15964 KB |
Output is correct |
2 |
Correct |
85 ms |
102748 KB |
Output is correct |
3 |
Correct |
479 ms |
52904 KB |
Output is correct |
4 |
Correct |
99 ms |
102740 KB |
Output is correct |
5 |
Correct |
73 ms |
106584 KB |
Output is correct |
6 |
Correct |
476 ms |
99084 KB |
Output is correct |
7 |
Correct |
50 ms |
37396 KB |
Output is correct |
8 |
Correct |
420 ms |
52928 KB |
Output is correct |
9 |
Correct |
705 ms |
79748 KB |
Output is correct |
10 |
Correct |
83 ms |
91220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
4 ms |
9052 KB |
Output is correct |
6 |
Correct |
3 ms |
8028 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
2 ms |
1116 KB |
Output is correct |
9 |
Correct |
5 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
1624 KB |
Output is correct |
11 |
Correct |
61 ms |
15964 KB |
Output is correct |
12 |
Correct |
85 ms |
102748 KB |
Output is correct |
13 |
Correct |
479 ms |
52904 KB |
Output is correct |
14 |
Correct |
99 ms |
102740 KB |
Output is correct |
15 |
Correct |
73 ms |
106584 KB |
Output is correct |
16 |
Correct |
476 ms |
99084 KB |
Output is correct |
17 |
Correct |
50 ms |
37396 KB |
Output is correct |
18 |
Correct |
420 ms |
52928 KB |
Output is correct |
19 |
Correct |
705 ms |
79748 KB |
Output is correct |
20 |
Correct |
83 ms |
91220 KB |
Output is correct |
21 |
Runtime error |
3 ms |
4700 KB |
Execution killed with signal 11 |
22 |
Halted |
0 ms |
0 KB |
- |