E - Cup 3 dp记忆化搜索

Description

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
***********************************************************************************************************************************************************
***********************************************************************************************************************************************************
 1 /*
 2 本题用记忆化搜索,其中a[i][j]表示前i个区域放置j个球迷的总方案数
 3 然后依次枚举每次放置可能出现的情况。
 4 用到了滚动数组 
 5 */
 6 #include<iostream>
 7 #include<string>
 8 #include<cstring>
 9 #include<cmath>
10 #include<cstdio>
11 using namespace std;
12 #define MOD  1000000007
13 #define maxn 100001
14 int a[2][maxn];
15 int k[maxn];
16 int s1,s2,n;
17 void dp(int ind)
18 {
19     int d=ind%2;
20     int s=1-d;
21     memset(a[d],0,sizeof(a)/2);
22     for(int it=0;it<=s1;it++)
23     {
24         if(a[s][it]>0)
25         {
26             if(it+k[ind]<=s1)
27             {
28                 a[d][it+k[ind]]+=a[s][it];
29                 if(a[d][it+k[ind]]>MOD)
30                   a[d][it+k[ind]]%=MOD;
31             }
32             if(k[ind]%2==0&&it+k[ind]/2<=s1)
33             {
34                 a[d][it+k[ind]/2]+=a[s][it];
35                 if(a[d][it+k[ind]/2]>MOD)
36                   a[d][it+k[ind]/2]%=MOD;
37             }
38             a[d][it]+=a[s][it];
39             if(a[d][it]>MOD)
40               a[d][it]%=MOD;
41         }
42 
43     }
44     if(ind<n)
45         dp(ind+1);
46 
47 }
48 int main()
49 {
50     int i;
51     while(scanf("%d %d",&s1,&s2)!=EOF)
52     {
53         scanf("%d",&n);
54         memset(a,0,sizeof(a));
55         if(s1>s2)
56          s1=s2;
57         for(i=0;i<n;i++)
58          scanf("%d",&k[i]);
59         a[0][0]=1;
60         a[0][k[0]]=1;
61         if(k[0]%2==0)
62           a[0][k[0]/2]=1;
63         dp(1);
64         printf("%d\n",a[1-(n%2)][s1]);
65     }
66     return 0;
67 }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3445447.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值