#include<bits/stdc++.h>
#define fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=50+10;
const int SS=1<<19;
const int INFi=2e9;
const ll INFl=1e13;
const ll mod2=998244353;
const ll mod=1e9+7;
const ll mod3=1000696969;
const ll p=70032301;
const ull p2=913;
const int L=20;
int t[N];
ll dp1[N][10*1000+10][2][2],dp2[N][10*1000+10][2][2],sp1[N][10*1000+10][2][2],sp2[N][10*1000+10][2][2];
void solve(){
int n,nk;
cin>>n>>nk;
for(int i=1;i<=n;t[i]--,i++) cin>>t[i];
if(1==n){
cout<<nk<<"\n";
return;
}
sort(t+1,t+1+n);
int akt=0;
for(int i=1;i<=n;i++) akt+=t[i]+1;
dp1[0][0][1][1]=1;
for(int i=1;i<=n;i++){
akt-=(t[i]+1);
int k=nk;
for(int i2=0;i2<=i;i2++){
for(int k1=0;k1<2;k1++) for(int k2=0;k2<2;k2++){
sp1[i2][0][k1][k2]=dp1[i2][0][k1][k2];
for(int sum=1;sum<=k;sum++)
(sp1[i2][sum][k1][k2]=sp1[i2][sum-1][k1][k2]+dp1[i2][sum][k1][k2])%=mod;
ll curr=dp1[i2][0][k1][k2];
sp2[i2][0][k1][k2]=dp1[i2][0][k1][k2];
for(int sum=1;sum<=k;sum++)
(curr+=dp1[i2][sum][k1][k2])%=mod,(sp2[i2][sum][k1][k2]=sp2[i2][sum-1][k1][k2]+curr)%=mod;
}
}
//nowa spojna
for(int i2=1;i2<=i;i2++){
for(int sum=1;sum<=k;sum++){
(dp2[i2][sum][0][0]=dp1[i2-1][sum-1][0][0]*(ll)(i2-2)+sp1[i2-1][sum-1][1][0]+sp1[i2-1][sum-1][0][1]);
(dp2[i2][sum][1][0]=dp1[i2-1][sum-1][1][0]*(ll)(i2-1)+sp1[i2-1][sum-1][1][1]);
(dp2[i2][sum][1][1]=dp1[i2-1][sum-1][1][1]*(ll)(i2));
(dp2[i2][sum][0][1]=dp1[i2-1][sum-1][0][1]*(ll)(i2-1)+sp1[i2-1][sum-1][1][1]);
}
}
//na koniec spojnej
for(int i2=1;i2<=i;i2++){
for(int sum=t[i]+1;sum<=k;sum++){
for(int k1=0;k1<2;k1++) for(int k2=0;k2<2;k2++){
(dp2[i2][sum][k1][k2]+=sp1[i2][sum-t[i]-1][k1][k2]*(ll)(i2*2-(k1^1)-(k2^1)));
if(!k1) (dp2[i2][sum][k1][k2]+=sp2[i2][sum-t[i]-1][1][k2]);
if(!k2) (dp2[i2][sum][k1][k2]+=sp2[i2][sum-t[i]-1][k1][1]);
}
}
}
//laczenie spojnych
for(int i2=1;i2<i;i2++){
for(int sum=(t[i]<<1)+1;sum<=k;sum++){
for(int k1=0;k1<2;k1++) for(int k2=0;k2<2;k2++){
(dp2[i2][sum][k1][k2]+=sp2[i2+1][sum-(t[i]<<1)-1][k1][k2]*(ll)(i2));
}
}
}
for(int i2=0;i2<=i;i2++){
for(int sum=0;sum<=k;sum++){
for(int k1=0;k1<2;k1++) for(int k2=0;k2<2;k2++){
dp1[i2][sum][k1][k2]=dp2[i2][sum][k1][k2]%mod;
dp2[i2][sum][k1][k2]=0;
}
}
}
}
cout<<dp1[1][nk][0][0]<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
int tt=1;
while(tt--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
782 ms |
64372 KB |
Output is correct |
2 |
Correct |
4 ms |
1492 KB |
Output is correct |
3 |
Correct |
40 ms |
12924 KB |
Output is correct |
4 |
Correct |
9 ms |
6496 KB |
Output is correct |
5 |
Correct |
39 ms |
8804 KB |
Output is correct |
6 |
Correct |
238 ms |
33752 KB |
Output is correct |
7 |
Correct |
358 ms |
34128 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
161 ms |
21868 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
14048 KB |
Output is correct |
2 |
Correct |
4 ms |
3028 KB |
Output is correct |
3 |
Correct |
39 ms |
14052 KB |
Output is correct |
4 |
Correct |
7 ms |
5208 KB |
Output is correct |
5 |
Correct |
42 ms |
14028 KB |
Output is correct |
6 |
Correct |
8 ms |
4308 KB |
Output is correct |
7 |
Correct |
39 ms |
14088 KB |
Output is correct |
8 |
Correct |
4 ms |
1748 KB |
Output is correct |
9 |
Correct |
7 ms |
4296 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2004 KB |
Output is correct |
2 |
Correct |
3 ms |
1236 KB |
Output is correct |
3 |
Correct |
8 ms |
2004 KB |
Output is correct |
4 |
Correct |
4 ms |
1492 KB |
Output is correct |
5 |
Correct |
8 ms |
2004 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
8 ms |
2004 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
4 ms |
1312 KB |
Output is correct |
10 |
Correct |
2 ms |
864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
782 ms |
64372 KB |
Output is correct |
2 |
Correct |
4 ms |
1492 KB |
Output is correct |
3 |
Correct |
40 ms |
12924 KB |
Output is correct |
4 |
Correct |
9 ms |
6496 KB |
Output is correct |
5 |
Correct |
39 ms |
8804 KB |
Output is correct |
6 |
Correct |
238 ms |
33752 KB |
Output is correct |
7 |
Correct |
358 ms |
34128 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
161 ms |
21868 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
38 ms |
14048 KB |
Output is correct |
12 |
Correct |
4 ms |
3028 KB |
Output is correct |
13 |
Correct |
39 ms |
14052 KB |
Output is correct |
14 |
Correct |
7 ms |
5208 KB |
Output is correct |
15 |
Correct |
42 ms |
14028 KB |
Output is correct |
16 |
Correct |
8 ms |
4308 KB |
Output is correct |
17 |
Correct |
39 ms |
14088 KB |
Output is correct |
18 |
Correct |
4 ms |
1748 KB |
Output is correct |
19 |
Correct |
7 ms |
4296 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
8 ms |
2004 KB |
Output is correct |
22 |
Correct |
3 ms |
1236 KB |
Output is correct |
23 |
Correct |
8 ms |
2004 KB |
Output is correct |
24 |
Correct |
4 ms |
1492 KB |
Output is correct |
25 |
Correct |
8 ms |
2004 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
8 ms |
2004 KB |
Output is correct |
28 |
Correct |
2 ms |
852 KB |
Output is correct |
29 |
Correct |
4 ms |
1312 KB |
Output is correct |
30 |
Correct |
2 ms |
864 KB |
Output is correct |
31 |
Correct |
795 ms |
64228 KB |
Output is correct |
32 |
Correct |
488 ms |
43292 KB |
Output is correct |
33 |
Correct |
794 ms |
64212 KB |
Output is correct |
34 |
Correct |
163 ms |
24812 KB |
Output is correct |
35 |
Correct |
801 ms |
64180 KB |
Output is correct |
36 |
Correct |
51 ms |
13516 KB |
Output is correct |
37 |
Correct |
802 ms |
64176 KB |
Output is correct |
38 |
Correct |
110 ms |
11596 KB |
Output is correct |
39 |
Correct |
801 ms |
64208 KB |
Output is correct |
40 |
Correct |
239 ms |
34300 KB |
Output is correct |
41 |
Correct |
772 ms |
64164 KB |
Output is correct |
42 |
Correct |
6 ms |
3028 KB |
Output is correct |
43 |
Correct |
790 ms |
64168 KB |
Output is correct |
44 |
Correct |
81 ms |
17100 KB |
Output is correct |
45 |
Correct |
786 ms |
64280 KB |
Output is correct |
46 |
Correct |
4 ms |
3924 KB |
Output is correct |
47 |
Correct |
211 ms |
31060 KB |
Output is correct |
48 |
Correct |
85 ms |
12044 KB |
Output is correct |
49 |
Correct |
17 ms |
8660 KB |
Output is correct |
50 |
Correct |
2 ms |
1108 KB |
Output is correct |