#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 110;
const int MOD = 1e9+7;
int dp[MAXN][MAXN][1010][5];
int arr[MAXN];
int n,L;
int rec(int i,int j,int k,int l){
//dp[i][j][k][l] = first i numbers, j connected components, k = total sum and l = number of endpoints 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];
}
int 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 endpoint
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 endpoint
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(int i=1;i<=n;i++){
cin>>arr[i];
}
sort(arr+1,arr+n+1);
for(int i=0;i<=n;i++){
for(int j=0;j<=n;j++){
for(int k=0;k<=L;k++){
for(int l=0;l<=3;l++){
dp[i][j][k][l] = -1;
}
}
}
}
cout<<rec(0,0,0,0)<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1280 KB |
Output is correct |
2 |
Correct |
6 ms |
1792 KB |
Output is correct |
3 |
Correct |
5 ms |
1536 KB |
Output is correct |
4 |
Correct |
5 ms |
1664 KB |
Output is correct |
5 |
Correct |
5 ms |
1664 KB |
Output is correct |
6 |
Correct |
5 ms |
1664 KB |
Output is correct |
7 |
Correct |
5 ms |
1408 KB |
Output is correct |
8 |
Correct |
5 ms |
1408 KB |
Output is correct |
9 |
Correct |
5 ms |
1664 KB |
Output is correct |
10 |
Correct |
5 ms |
1664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |