#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 110;
const long long MOD = 1e9+7;
long long dp[MAXN][MAXN][1010][5];
long long arr[MAXN];
long long n,L;
long long rec(long long i,long long j,long long k,long long l){
//dp[i][j][k][l] = first i numbers, j connected components, k = total sum and l = number of endpolong longs on which numbers have been placed
if(i>0 && (j<1||l>2||k>L)){
return 0;
}
if(i==n){
return (j==1 && l==2);
}
if(dp[i][j][k][l]!=-1){
return dp[i][j][k][l];
}
long long toadd = (arr[i+1]-arr[i])*(2*j-l) + k;//all positions adjacent to existing connected components must be incremented by the difference between adjacent elements in our array
dp[i][j][k][l] = 0;
dp[i][j][k][l] = (dp[i][j][k][l] + ((2*j-l)%MOD*rec(i+1,j,toadd,l)%MOD)%MOD)%MOD;//add on to an already existing connected component
dp[i][j][k][l] = (dp[i][j][k][l] + ((j+1-l)%MOD*rec(i+1,j+1,toadd,l)%MOD)%MOD)%MOD;//add a new connected component
dp[i][j][k][l] = (dp[i][j][k][l] + ((2-l)%MOD*rec(i+1,j+1,toadd,l+1)%MOD)%MOD)%MOD;//add a new connected component to an endpolong long
dp[i][j][k][l] = (dp[i][j][k][l] + ((2-l)%MOD*rec(i+1,j,toadd,l+1)%MOD)%MOD)%MOD;//extend an already existing connected component to an endpolong long
dp[i][j][k][l] = (dp[i][j][k][l] + ((j-1)%MOD*rec(i+1,j-1,toadd,l)%MOD)%MOD)%MOD;//merge 2 connected components
return dp[i][j][k][l];
}
int main(){
cin>>n>>L;
for(long long i=1;i<=n;i++){
cin>>arr[i];
}
sort(arr+1,arr+n+1);
for(long long i=0;i<=n;i++){
for(long long j=0;j<=n;j++){
for(long long k=0;k<=L;k++){
for(long long l=0;l<=3;l++){
dp[i][j][k][l] = -1;
}
}
}
}
if(n==1){
cout<<1<<endl;
return 0;
}
cout<<rec(0,0,0,0)<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
768 KB |
Output is correct |
5 |
Correct |
6 ms |
3584 KB |
Output is correct |
6 |
Correct |
6 ms |
3456 KB |
Output is correct |
7 |
Correct |
5 ms |
1152 KB |
Output is correct |
8 |
Correct |
5 ms |
896 KB |
Output is correct |
9 |
Correct |
6 ms |
3584 KB |
Output is correct |
10 |
Correct |
5 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1536 KB |
Output is correct |
2 |
Correct |
5 ms |
2176 KB |
Output is correct |
3 |
Correct |
5 ms |
1664 KB |
Output is correct |
4 |
Correct |
5 ms |
2176 KB |
Output is correct |
5 |
Correct |
5 ms |
2176 KB |
Output is correct |
6 |
Correct |
5 ms |
2048 KB |
Output is correct |
7 |
Correct |
5 ms |
1536 KB |
Output is correct |
8 |
Correct |
5 ms |
1664 KB |
Output is correct |
9 |
Correct |
5 ms |
1920 KB |
Output is correct |
10 |
Correct |
6 ms |
2048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
768 KB |
Output is correct |
5 |
Correct |
6 ms |
3584 KB |
Output is correct |
6 |
Correct |
6 ms |
3456 KB |
Output is correct |
7 |
Correct |
5 ms |
1152 KB |
Output is correct |
8 |
Correct |
5 ms |
896 KB |
Output is correct |
9 |
Correct |
6 ms |
3584 KB |
Output is correct |
10 |
Correct |
5 ms |
1024 KB |
Output is correct |
11 |
Correct |
5 ms |
1536 KB |
Output is correct |
12 |
Correct |
5 ms |
2176 KB |
Output is correct |
13 |
Correct |
5 ms |
1664 KB |
Output is correct |
14 |
Correct |
5 ms |
2176 KB |
Output is correct |
15 |
Correct |
5 ms |
2176 KB |
Output is correct |
16 |
Correct |
5 ms |
2048 KB |
Output is correct |
17 |
Correct |
5 ms |
1536 KB |
Output is correct |
18 |
Correct |
5 ms |
1664 KB |
Output is correct |
19 |
Correct |
5 ms |
1920 KB |
Output is correct |
20 |
Correct |
6 ms |
2048 KB |
Output is correct |
21 |
Correct |
9 ms |
9856 KB |
Output is correct |
22 |
Correct |
212 ms |
226552 KB |
Output is correct |
23 |
Correct |
262 ms |
403960 KB |
Output is correct |
24 |
Correct |
235 ms |
337784 KB |
Output is correct |
25 |
Correct |
265 ms |
403960 KB |
Output is correct |
26 |
Correct |
229 ms |
378872 KB |
Output is correct |
27 |
Correct |
103 ms |
139512 KB |
Output is correct |
28 |
Correct |
113 ms |
161656 KB |
Output is correct |
29 |
Correct |
196 ms |
270072 KB |
Output is correct |
30 |
Correct |
274 ms |
403960 KB |
Output is correct |