//Be Name KHODA
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pp pop_back
#define lb lower_bound
#define ub upper_bound
#define cl clear
#define bg begin
#define arr(x) array<int , x>
#define endl '\n'
const int MOD = int(1e9) + 7;
int n , k;
vector<int> a;
int dp[(1 << 14)][14][101];
void Add(int &e , int b){
e += b;
if(e > MOD) e -= MOD;
if(e < 0) e += MOD;
}
int Mul(int a , int b){
return (1ll * a * b) % MOD;
}
int Pow(int a , int b){
int res = 1;
while(b){
if((b & 1)) res = Mul(res , a);
a = Mul(a , a) , b >>= 1;
}
return res;
}
void f(){
for(int bit = 0 ; bit < n ; bit++){
dp[(1 << bit)][bit][0] = 1;
}
for(int msk = 1 ; msk < (1 << 14) ; msk++){
for(int bit0 = 0 ; bit0 < n ; bit0++){
if(((msk >> bit0) & 1)) continue;
for(int bit1 = 0 ; bit1 < n ; bit1++){
if(!((msk >> bit1) & 1)) continue;
for(int sm = 0 ; sm <= k ; sm++){
if(sm + abs(a[bit0] - a[bit1]) > k) continue;
Add(dp[msk | (1 << bit0)][bit0][sm + abs(a[bit0] - a[bit1])] , dp[msk][bit1][sm]);
}
}
}
}
}
int fac(int e){
int res = 1;
for(int i = 2 ; i <= e ; i++) res *= i;
return res;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for(int i = 0 ; i < n ; i++){
int d;
cin >> d;
a.pb(d);
}
int o = 0;
if(n <= 8){
for(int i = 0 ; i < fac(n) ; i++){
int sm = 0;
for(int j = 1 ; j < n ; j++) sm += abs(a[j] - a[j - 1]);
if(sm <= k) o++;
next_permutation(a.bg() , a.end());
}
}else{
f();
for(int end = 0 ; end < n ; end++){
for(int sm = 0 ; sm <= k ; sm++){
Add(o , dp[(1 << n) - 1][end][sm]);
}
}
}
cout << o;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
90800 KB |
Output is correct |
2 |
Correct |
171 ms |
91020 KB |
Output is correct |
3 |
Correct |
118 ms |
90984 KB |
Output is correct |
4 |
Correct |
173 ms |
90992 KB |
Output is correct |
5 |
Correct |
179 ms |
90884 KB |
Output is correct |
6 |
Correct |
183 ms |
90964 KB |
Output is correct |
7 |
Correct |
92 ms |
90964 KB |
Output is correct |
8 |
Correct |
117 ms |
90964 KB |
Output is correct |
9 |
Correct |
160 ms |
90844 KB |
Output is correct |
10 |
Correct |
158 ms |
90844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
105 ms |
90800 KB |
Output is correct |
12 |
Correct |
171 ms |
91020 KB |
Output is correct |
13 |
Correct |
118 ms |
90984 KB |
Output is correct |
14 |
Correct |
173 ms |
90992 KB |
Output is correct |
15 |
Correct |
179 ms |
90884 KB |
Output is correct |
16 |
Correct |
183 ms |
90964 KB |
Output is correct |
17 |
Correct |
92 ms |
90964 KB |
Output is correct |
18 |
Correct |
117 ms |
90964 KB |
Output is correct |
19 |
Correct |
160 ms |
90844 KB |
Output is correct |
20 |
Correct |
158 ms |
90844 KB |
Output is correct |
21 |
Runtime error |
12 ms |
25496 KB |
Execution killed with signal 11 |
22 |
Halted |
0 ms |
0 KB |
- |