Candy Distribution Time limit:4000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): PNs Accepted Submission (s): 7
Problem Description WY has n kind of candy, number 1-n, the i-th kind of candy have AI. WY would like to give some of the candy to his teammate Ecry and Lasten. To is fair, he hopes that Ecry ' s candies is as many as Lasten ' in the end. How many kinds of methods is there?
Input The first line contains a integer t<=11 which is the number of the test cases.
Then T cases follow. Each case contains the lines. The first line contains one integer n (1<=n<=200). The second line contains n integers ai (1<=ai<=200)
Output for each test case, output a single integer (the number of ways this WY can distribute candies to his teammates, MO Dulo 9+7) in a single line.
Sample Input
2 1 2 2 1 2
Sample Output
2 4 Hint sample:a total of 4, (1) ecry and Lasten is not assigned to the candy; (2) Ecry and Lasten each to a second kind of candy; (3) Ecry points to one of the first kind of candy, Lasten points to a second type of candy; (4) Ecry points to a second type of candy, Lasten points to one of the first kind of candy.
Author FZUACM
Source multi-university Training Contest 1
Test instructions: There are sweets in N, each kind of candy has an AI. Assign to a A, a two person. Two people's candy to be as many as, can all be a 0,1......m. There is no difference between the same candy.
There are several points to ask.
Define Dp[i] Indicates the number of cases in which I have a candy difference between two people. Treat each type of candy *dp[i] represents the newly computed DP value
When there is currently AI I candy. Handling *dp[j]
*DP[J] = dp[j]* (ai/2+1) + dp[j-1]* ((ai-1)/2+1) + dp[j+1]* ((ai-1)/2+1) ... +dp[j-ai]* ((Ai-ai)/2+1) + dp[j+ai]* (( Ai-ai)/2+1)
Said Dp[j-k]* ((ai-k)/2+1) said that the original a A, a difference j-k candy, but by the sub-I candy, first gave a cent k candy, so that the rest of the candy to be equally divided to a, B. After that, the difference between J Candy. Due to the advance to a,k a candy, then the rest of Ai-k Candy, split the rest of the situation has (ai-k)/2+1 species. +1 is two everyone is divided into 0.
Suppose ai = 2 j = 0, then dp[0] =
Dp:dp[-2] dp[-1] dp[0] dp[1] dp[2]
Coefficient: 1 1 2 1 1
Calculate *dp[0], calculate *dp[1] = *dp[0] + dp[1] + dp[3]-dp[0]-dp[-2]
It can be found that at this point the [J+1,j+1+ai] of the odd position of the DP value up-[j-ai,j] Even position of the DP value + *dp[0] = *dp[1]
The transfer becomes O (1).
Suppose ai = 3 j = 0, then dp[0] =
Dp:dp[-3] dp[-2] dp[-1] dp[0] dp[1] dp[2] dp[3]
Coefficient: 1 1 2 2 2 1 1
With AI = 2 instead. To find the law can.
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace
Std
#define LL Long long int dp[90000],sum[2][90000];
int mod = 1000000007;
int modx =-mod;
int num[300];
int main () {int t,n;
scanf ("%d", &t);
while (t--) {scanf ("%d", &n);
int total = 0;
for (int i = 1; I <= n; i++) {scanf ("%d", &num[i]);
Total + = Num[i];
} if (total & 1) total++;
Memset (Dp,0,sizeof (DP));
memset (sum,0,sizeof (sum));
Dp[total] = 1;
int tt = TOTAL*2;
for (int i = 1; I <= n; i++) {sum[0][0] = dp[0];
Sum[1][0] = 0;
for (int j = 1;j <= tt; j + +) {Sum[0][j] = sum[0][j-1];
SUM[1][J] = sum[1][j-1];
SUM[J&1][J] + = dp[j];
SUM[J&1][J]%= MODx;
} ll ans = 0;
for (int j = 0;j <= num[i]; j + +) { Ans + = (ll) dp[j]* ((num[i]-j)/2+1);
Ans%= MoD;
} int p = (num[i]&1) ^1;
int res = ans;
for (int j = 0;j <= tt; j + +) {Dp[j] = res;
int u = j-num[i]-1;
u = max (u,0);
Res + = (Sum[p][j+1+num[i]]-sum[p][j])%mod;
Res%= mod;
P ^= 1;
Res-= (Sum[p][j]-sum[p][u])%mod;
Res%= mod;
}} int res = dp[total];
Res%= mod;
res = (res+mod)%mod;
cout<<res<<endl;
} return 0;
}