[dp] zoj 3682 E - Cup 3

题目链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3682

E - Cup 3


Time Limit: 3 Seconds      Memory Limit: 65536 KB



The 2012 Europe Cup was over and Spain won the Championship. The fans of Spain want to hold some activities to celebrate. But there is a question. Some of them are the fans of F.C Barcelona
and the others are Real Madrid. It is well-known that the relation of these two clubs is very bad. So they may have some disharmony events when the fans of the two clubs celebrate together.

There are s1 fans of Barcelona and s2 fans of Madrid (0 ≤ s1, s2 ≤ 100000). The government provided n (2 ≤ n ≤ 200) squares and each square can contain ki fans. Only if each square
is all Barcelona‘s fans or Madrid‘s fans or the number of two clubs is equal exactly, it is harmony.

Your task is calculating the sum of ways that the celebration is harmony.

Input

There are multiple test cases(no more than 50). For each test case:

The first line contains two integers s1 and s2 (0 ≤ s1, s2 ≤ 100000), the number of fans of Barcelona and Madrid. The second line contains one integer n (2 ≤ n ≤ 200), the number of squares. The third line contains n integers,
the ith integer is ki (1 ≤ i ≤ n). We promise that the sum of ki is equal to the sum of s1 and s2.The sum will modulo 1000000007.

Output

In each test case, output the sum of ways.

Sample Input

2 1
2
2 1

Sample Output

2

Reference

http://en.wikipedia.org/wiki/FC_Barcelona

http://en.wikipedia.org/wiki/Real_Madrid


Author: ZHOU, Xiao

Contest: ZOJ Monthly, January 2013

Submit    Status

题目意思:

有s1个人支持球队1,s2个人支持球队2,有n个房子,每个房子可容纳的人数为ki, 满足sum(ki)=s1+s2。每个房子要么全部容纳支持球队1的人,要么全部容纳支持球队2的人,要么一半是支持球队1的一半是支持球队2的,求分配的方案数。

解题思路:

dp.

dp[i][j]:表示分完前i个房子后支持球队1的还剩下j个人的方案总数。因为分完前i个房子后,后面需要分的人的总数是确定的,知道支持球队1的人数后,剩下的就是支持球队2的。

显然对于每个房子要么全部分配给球队1,要么全部分配给球队2,如果是偶数的话还可以两支球队各分一半。

dp[i][j]=max(dp[i-1][j+save[i]],dp[i-1][j],dp[i-1][j+save[i]/2).

代码:

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>

#include<cmath>
#define ll long long
#include<cstdlib>

using namespace std;
#define M 1000000007
#define Maxn 210000
ll dp[2][Maxn];
int n,save[220],sum[220];
ll s1,s2;
int zong;

int main()
{
    while(~scanf("%lld%lld",&s1,&s2))
    {
        scanf("%d",&n);
        sum[0]=0;
        zong=0;

        for(int i=1;i<=n;i++)
        {
            scanf("%d",&save[i]);
            zong+=save[i];
            sum[i]=sum[i-1]+save[i];
        }

        if(s1==0||s2==0)
        {
            printf("1\n");
            continue;
        }
        memset(dp,0,sizeof(dp));

        dp[0][s1]=1; //第一个人剩下s1
        int cur=0;

        for(int i=1;i<=n;i++)
        {
            cur=cur^1;
            memset(dp[cur],0,sizeof(dp[cur]));
            int lim=zong-sum[i];

            //if(save[i]&1)
            {
                for(int j=lim;j>=0;j--)
                {
                    dp[cur][j]=(dp[cur][j]+dp[cur^1][j+save[i]])%M;
                    dp[cur][j]=(dp[cur][j]+dp[cur^1][j])%M;
                }
            }
            if(!(save[i]&1))
            {
                for(int j=lim;j>=0;j--)
                {
                    dp[cur][j]=(dp[cur][j]+dp[cur^1][j+save[i]/2])%M;
                }
            }
            /*for(int j=lim;j>=0;j--)
            {
                printf("i:%d j:%d %lld\n",i,j,dp[cur][j]);
                system("pause");
            }*/
        }
        printf("%lld\n",dp[cur][0]);

    }
    return 0;
}
/*
2 2
3
1 1 2
*/

[dp] zoj 3682 E - Cup 3

时间: 2024-08-10 21:29:33

[dp] zoj 3682 E - Cup 3的相关文章

[dp] zoj 3769 Diablo III

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3769 Diablo III Time Limit: 2 Seconds      Memory Limit: 65536 KB Diablo III is an action role-playing video game. A few days ago, Reaper of Souls (ROS), the new expansion of Diablo I

[dp] zoj 3740 Water Level

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5122 Water Level Time Limit: 2 Seconds      Memory Limit: 65536 KB Hangzhou is a beautiful city, especially the West Lake. Recently, the water level of the West Lake got lower and lower

[记忆化搜索] zoj 3681 E - Cup 2

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3681 E - Cup 2 Time Limit: 2 Seconds      Memory Limit: 65536 KB The European Cup final is coming. The past two World Cup winners, Spain and Italy, will contest the decider at Kiev's

[递推dp] zoj 3747 Attack on Titans

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5170 Attack on Titans Time Limit: 2 Seconds      Memory Limit: 65536 KB Over centuries ago, mankind faced a new enemy, the Titans. The difference of power between mankind and their newf

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

(状态压缩DP) zoj 3471

T - Most Powerful Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3471 Appoint description:  System Crawler  (2015-04-16) Description Recently, researchers on Mars have discovered N powerful atoms

状压DP [ZOJ 3471] Most Powerful

Most Powerful Time Limit: 2 Seconds      Memory Limit: 65536 KB Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These atoms have some properties. When two of these atoms collide, one of them disappears and a

DP ZOJ 3872 Beauty of Array

题目传送门 1 /* 2 DP:dp 表示当前输入的x前的包含x的子序列的和, 3 求和方法是找到之前出现x的位置(a[x])的区间内的子序列: 4 sum 表示当前输入x前的所有和: 5 a[x] 表示id: 6 详细解释:http://blog.csdn.net/u013050857/article/details/45285515 7 */ 8 #include <cstdio> 9 #include <algorithm> 10 #include <cmath>

(环形DP) zoj 3310

W - Unrequited Love Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3310 Appoint description:  System Crawler  (2015-04-16) Description Owen had been through unrequited love with N MM for a long t