Each goat has n attributes.
The following n numbers indicate that the value range of each attribute is [0, T [I].
If each attribute of goat A in the sheepfold is greater than or equal to that of goat B, goat A will kill goat B.
Ask how many sheep can survive in the sheepfold.
Rule 1: sheep with the same sum will not kill each other.
Because if the attributes of two lambs are the same and a certain attribute of goat A needs to be increased by 1, another attribute of goat A needs to be reduced by 1, so that AB can coexist.
Rule 2:
The sheep with different sum will not overlap.
We set a goat sum = X, B goat sum = Y. If a and B goat can coexist, but AB will not be placed in the sheepfold at the same time.
Because there must be a sheep C, sum = x, and C and B cannot coexist. Since they cannot coexist, putting C in the sheep will not affect the answer.
Therefore, DP [I] [J] indicates the number of solutions when sum of the first I sheep is J.
However, the result is mod, so the maximum value cannot be obtained for all sums.
We can find that sum = 0 and sum = sum (T [I]) have the same number of solutions.
Similarly, sum is symmetric, which is the same as the number of combinations. Therefore, DP [N] [sum (T [I])/2] is the largest.
#include <iostream>#include <cstdio>#include <string.h>#include <queue>#include <vector>#include <algorithm>#include <set>using namespace std;#define N 2005typedef long long ll;const ll mod = 1000000007LL;int n;ll dp[N][N];int a[N],sum;ll solve(){if(n == 1)return 1LL;memset(dp, 0, sizeof dp);for(int i = 0; i <= a[1]; i++)dp[1][i] = 1;for(int i = 2; i <= n; i++){for(int j = 0; j <= sum; j++) {for(int k = 0; k + j <= sum && k <= a[i]; k++){dp[i][k+j] = (dp[i][k+j] + dp[i-1][j]) % mod;}}}return dp[n][sum];}int main(){int T;scanf("%d",&T);while(T--){scanf("%d", &n);for(int i = 1; i <= n; i++)scanf("%d", &a[i]);sum = 0;for(int i = 1; i <= n; i++)sum += a[i];sum /= 2;cout<<solve()<<endl;}return 0;}/*991528 633 2 2 32 2 2*/
HDU 5000 clone rule + dp 2014 ACM/ICPC Asia Regional Anshan online