#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) x.begin(),x.end()
#define ff(i,a,b) for (int i = a; i < b; i++)
#define fff(i,a,b) for (int i = a; i <= b; i++)
#define bff(i,a,b) for (int i = b-1; i >= a; i--)
#define bfff(i,a,b) for (int i = b; i >= a; i--)
using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
pair<ld,ld> typedef pld;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
//const int mod = 998244353;
const int mod = 1000000007;
//Note to self: Check for overflow
int a[105];
int dp[16999][16][203];
void solve_megabrute(int n, int L)
{
sort(a,a+n);
int ans=0;
do
{
int tmp=0;
ff(i,1,n) tmp+=abs(a[i]-a[i-1]);
if (tmp<=L) ans++;
} while (next_permutation(a,a+n));
cout<<ans<<"\n";
}
void solve_bitmask(int n, int L)
{
ff(i,0,n) dp[(1<<i)][i][0]=1;
ff(mask,0,(1<<n)) ff(lst,0,n) ff(dole,0,n) fff(t,0,L) if (((mask>>lst)&1) && ((mask>>dole)&1) && lst!=dole)
{
(dp[mask][lst][t+abs(a[lst]-a[dole])]+=dp[mask^(1<<lst)][dole][t])%=mod;
}
int sms=(1<<n)-1;
int ans=0;
ff(i,0,n) fff(t,0,L) (ans+=dp[sms][i][t])%=mod;
cout<<ans<<"\n";
}
int main()
{
FAST;
int n,L; cin>>n>>L;
ff(i,0,n) cin>>a[i];
if (n>14) return cout<<"necu radim",0;
if (n<=8) solve_megabrute(n,L);
else solve_bitmask(n,L);
}
//Note to self: Check for overflow
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
46464 KB |
Output is correct |
2 |
Correct |
680 ms |
201360 KB |
Output is correct |
3 |
Correct |
412 ms |
198416 KB |
Output is correct |
4 |
Correct |
678 ms |
201540 KB |
Output is correct |
5 |
Correct |
715 ms |
201740 KB |
Output is correct |
6 |
Correct |
669 ms |
200956 KB |
Output is correct |
7 |
Correct |
301 ms |
198176 KB |
Output is correct |
8 |
Correct |
406 ms |
198344 KB |
Output is correct |
9 |
Correct |
558 ms |
199172 KB |
Output is correct |
10 |
Correct |
592 ms |
201304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
212 KB |
Output is correct |
11 |
Correct |
99 ms |
46464 KB |
Output is correct |
12 |
Correct |
680 ms |
201360 KB |
Output is correct |
13 |
Correct |
412 ms |
198416 KB |
Output is correct |
14 |
Correct |
678 ms |
201540 KB |
Output is correct |
15 |
Correct |
715 ms |
201740 KB |
Output is correct |
16 |
Correct |
669 ms |
200956 KB |
Output is correct |
17 |
Correct |
301 ms |
198176 KB |
Output is correct |
18 |
Correct |
406 ms |
198344 KB |
Output is correct |
19 |
Correct |
558 ms |
199172 KB |
Output is correct |
20 |
Correct |
592 ms |
201304 KB |
Output is correct |
21 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |