Topic Link: Click to open the link
For n individuals, let's divide into groups, each of which is the maximum value minus the minimum value, and all the groups.
Idea: Obviously, the idea of DP is needed, but the design of the DP is very ingenious, and the situation of state transition is easy to consider.
We use D[I][J][V] to represent the number of the first I, the J Group is open (the so-called open refers to the minimum value, there is no maximum, but also to the person), the current value of the sum of the number of V.
We first sort, so that for the open group, each cumulative amount is j* (A[i]-a[i-1]).
So there are so many shifts to consider:
1. A group of the first number one group
2. Number one new group, as the minimum value of the new group
3. The number of I closes a group as the maximum value for this group.
4. The number of I entered a group of J groups.
You need to use a scrolling array, or you can explode memory.
Details see code:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include < cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue> # Define MAX (A,b) ((a) > (b)? ( A):(B) #define Min (A,b) ((a) < (b)? (
(a):(B)) using namespace Std;
typedef long Long LL;
Const double PI = ACOs (-1.0);
Const double EPS = 1e-6;
const INT mod = 1000000000 + 7;
const int INF = 1000000000;
const int MAXN = 200 + 10;
int T,N,K,A[MAXN], vis[maxn][maxn][1000 +], kase=1;
ll d[2][maxn][1000 + 10];
int main () {scanf ("%d%d", &n,&k);
for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
Sort (a+1, a+n+1);
A[0] = a[1];
int u = 0;
D[u][0][0] = 1;
for (int i=1;i<=n;i++) {u ^= 1;
memset (D[u], 0, sizeof (d[u)); for (int j=0;j<=n;j++) {int add = (a[I]-a[i-1]);
for (int v=0;v<=k;v++) {if (!d[u^1][j][v]) continue; if (v + j*add > k) break; Pruning, don't forget, take the mold is very time-consuming d[u][j][v+j*add] = (D[u][j][v+j*add] + d[u^1][j][v])%mod;//a group of their own d[u][j ][v+j*add] = (D[u][j][v+j*add] + d[u^1][j][v]*j%mod)%mod;//thrown randomly into a group of if (J > 0) {d[u][ J-1][v+j*add] = (D[u][j-1][v+j*add] + d[u^1][j][v]*j%mod)%mod;//closes a group as the end point} d[u][j+1][v+j*
Add] = (D[u][j+1][v+j*add] + d[u^1][j][v])%mod;//open a group, as the starting point}} ll ans = 0;
for (int i=0;i<=k;i++) {ans = (ans + d[u][0][i])%mod;
printf ("%i64d\n", ans);
return 0;
}