Sharing Chocolate-uva 1099-shape pressure DP

Source: Internet
Author: User

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
    1. #include <cstdio>
    2. #include <cstring>
    3. #include <algorithm>
    4. Using namespace std;
    5. int sum[100010],num[20],vis[110][100010],dp[110][100010],pow2[20];
    6. int n,t;
    7. int DFS (int c,int S)
    8. {
    9. if (vis[c][s]==t)
    10. return dp[c][s];
    11. int i,j,k,l,ans=0,s1,s2;
    12. vis[c][s]=t;
    13. For (j=0;j<n;j++)
    14. if (S==pow2[j])
    15. return dp[c][s]=1;
    16. L=SUM[S]/C;
    17. For (s1= (S-1) &S; s1>0; s1= (s1-1) &s)
    18. {
    19. S2=S-S1;
    20. if (sum[s1]%c==0 && dfs (min (c,sum[s1]/c), S1) && dfs (min (c,sum[s2]/c), S2))
    21. return dp[c][s]=1;
    22. if (sum[s1]%l==0 && dfs (min (l,sum[s1]/l), S1) && dfs (min (l,sum[s2]/l), S2))
    23. return dp[c][s]=1;
    24. }
    25. return dp[c][s]=0;
    26. }
    27. int main ()
    28. {
    29. int I,j,k,c,l,s,ans;
    30. Pow2[0]=1;
    31. For (i=1;i<=20;i++)
    32. pow2[i]=pow2[i-1]*2;
    33. while (~scanf ("%d", &n) && n>0)
    34. {
    35. t++;
    36. scanf ("%d%d", &c,&l);
    37. For (i=0;i<n;i++)
    38. scanf ("%d", &num[i]);
    39. S=pow2[n]-1;
    40. For (i=1;i<=s;i++)
    41. {
    42. sum[i]=0;
    43. For (j=0;j<n;j++)
    44. if (I&pow2[j])
    45. SUM[I]+=NUM[J];
    46. }
    47. if (C*l!=sum[s])
    48. ans=0;
    49. Else
    50. Ans=dfs (min (c,l), S);
    51. printf ("Case%d:", t);
    52. if (ans)
    53. printf ("yes\n");
    54. Else
    55. printf ("no\n");
    56. }
    57. }

Sharing Chocolate-uva 1099-shape pressure DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.