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

猜你喜欢

转载自blog.csdn.net/weixin_34235105/article/details/93432923
CUP