Chocolate in its many forms are enjoyed by millions of people around the world every day. It's a truly universal candy available in virtually every country around the world.
You find, the only thing better than eating chocolate are to share it with friends. Unfortunately your friends is very picky and has different appetites:some would like more and others less of the Chocol Ate that and offer them. You are found it increasingly difficult to determine whether their demands can be met. It is time-to-Writte a program, solves the problem once and for all!
Your chocolate comes as a rectangular bar. The bar consists of same-sized rectangular pieces. To share the chocolate, break one bar into a pieces along a division between rows or columns of the bar. You or the repeatedly break the resulting pieces in the same manner. Each of your friends insists in a getting a single rectangular portion of the chocolate so has a specified number of pie Ces. You are a little bit insistent as well:you would break up your bar only if all of the it can be distributed to your friends, W ith none left over.
For Exampla, Figure 9 shows one to that a chocolate bar consisting of 3 x 4 pieces can is split into 4 parts That's contain 6, 3, 2, and 1 pieces respectively, by breanking it 3 times (this corresponds to the first sample input.)
Input
The input consists of multiple test cases each describing a chocolate bar to share. Each description starts with a line containing a single integerN(1N15), the number of parts in which the bar was supposed to be split. This was followed by a line containing the integers x and y (1 x , y ) , the dimensions of the Chocolate bar. The next line Contains n positive integers, giving the number of pieces that is supposed to is in each of The n parts.
The input is terminated by a line containing the integer zero.
Output
For each test case, first display it case number. Then display whether it was possible to broke the chocolate in the desired way:display 'Yes' If it was possible, and 'No' otherwise. Follow the format of the sample output.
Sample Input
4 3 4 6 3 2 1 2 2 3 1 5 0
Sample Output
Case 1:yes Case 2:no
< Span style= "FONT-FAMILY:SIMSUN; font-size:14px; " >< Span style= "FONT-FAMILY:SIMSUN; font-size:14px; " >
Test instructions: Ask X*y whether the chocolate can be divided into a given n block size.
Idea: Dp[c][s] Indicates whether chocolate with a state of the same pressure can form a rectangle with a short edge of C, and then Dfs.
The AC code is as follows:
[CPP]View Plaincopy
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- Using namespace std;
- int sum[100010],num[20],vis[110][100010],dp[110][100010],pow2[20];
- int n,t;
- int DFS (int c,int S)
- {
- if (vis[c][s]==t)
- return dp[c][s];
- int i,j,k,l,ans=0,s1,s2;
- vis[c][s]=t;
- For (j=0;j<n;j++)
- if (S==pow2[j])
- return dp[c][s]=1;
- L=SUM[S]/C;
- For (s1= (S-1) &S; s1>0; s1= (s1-1) &s)
- {
- S2=S-S1;
- if (sum[s1]%c==0 && dfs (min (c,sum[s1]/c), S1) && dfs (min (c,sum[s2]/c), S2))
- return dp[c][s]=1;
- if (sum[s1]%l==0 && dfs (min (l,sum[s1]/l), S1) && dfs (min (l,sum[s2]/l), S2))
- return dp[c][s]=1;
- }
- return dp[c][s]=0;
- }
- int main ()
- {
- int I,j,k,c,l,s,ans;
- Pow2[0]=1;
- For (i=1;i<=20;i++)
- pow2[i]=pow2[i-1]*2;
- while (~scanf ("%d", &n) && n>0)
- {
- t++;
- scanf ("%d%d", &c,&l);
- For (i=0;i<n;i++)
- scanf ("%d", &num[i]);
- S=pow2[n]-1;
- For (i=1;i<=s;i++)
- {
- sum[i]=0;
- For (j=0;j<n;j++)
- if (I&pow2[j])
- SUM[I]+=NUM[J];
- }
- if (C*l!=sum[s])
- ans=0;
- Else
- Ans=dfs (min (c,l), S);
- printf ("Case%d:", t);
- if (ans)
- printf ("yes\n");
- Else
- printf ("no\n");
- }
- }
Sharing Chocolate-uva 1099-shape pressure DP