hdu 5291 Candy Distribution 2015 Multi-University Training Contest 1

Candy Distribution

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 37    Accepted Submission(s): 7


Problem Description
WY has n kind of candy, number 1-N, The i-th kind of candy has ai. WY would like to give some of the candy to his teammate Ecry and lasten. To be fair, he hopes that Ecry’s candies are as many as lasten's in the end. How many kinds of methods are there?
 

Input
The first line contains an integer T<=11 which is the number of test cases. 
Then  T  cases follow. Each case contains two 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 that WY can distribute candies to his teammates, modulo 10 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 are 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


题意:有n中糖果,每种糖果有ai个。分给A,B两个人。两人的糖果要一样多,可以都是0,1......m个。同一种糖果没有区别。

问有几种分法。


定义dp[i]表示两人之间相差i个糖果的情况数。对每种糖果进行处理  *dp[i]表示新计算得到的dp值

当当前有ai个i种糖果时。处理*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)

表示dp[j-k]*((ai-k)/2+1)表示原来A,B相差j-k个糖果,但是通过分第i种糖果,先给A分了k个糖果,让后剩下的糖果再平等地分给A,B。那么分完之后就相差j个糖果了。由于提前给A,k个糖果,那么剩下ai-k个糖果,平分剩下糖果的情况有(ai-k)/2+1种。+1是两个人人都分0个。

假设ai = 2 j = 0, 那么dp[0] = 

dp:      dp[-2]   dp[-1]  dp[0] dp[1] dp[2]

系数:1             1          2        1         1


算出*dp[0]之后,算*dp[1]  = *dp[0] + dp[1] + dp[3] - dp[0] - dp[-2]

可以发现此时只要把[j+1,j+1+ai]的奇数位置的dp值加起来 - [j-ai,j]偶数位置的dp值 + *dp[0] = *dp[1]

转移变成O(1)的了。



假设ai = 3 j = 0, 那么dp[0] = 

dp:     dp[-3]  dp[-2]   dp[-1]  dp[0] dp[1] dp[2]   dp[3]

系数:1             1          2        2         2         1         1


同ai = 2相反。找规律即可。


#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;
}















  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值