Uvalive-4794--sharing Chocolate

Source: Internet
Author: User

Title: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12055

Because of the n<=15, it can be obvious that the problem can be solved by state compression DP.

The crux of the problem is: How to design the DP state to achieve the goal, but also to reduce the time, space complexity.

The easiest to think of is: dp[set s][long x][width y]

But a careful thought will find, in fact, as long as you know the set of S and "long" and "wide" one can be pushed by two three!

Therefore, the state can be designed as: dp[set S][min (length x, width y)], not only reduce one dimension, but also remove the x, y symmetrical half of the redundant state!

#include <cstdio>#include<iostream>#include<cstring>using namespacestd;inta[ the];intarea[1<< -];intdp[1<< -][ the];intBitcount (intS) {    intRET =0;  while(S) {if(s&1) ret++; S>>=1; }        returnret;}BOOLDfsintSintx) {    if(dp[s][x]! =-1)returnDp[s][x]; if(Bitcount (S) = =1)returndp[s][x]=1; inty = area[s]/x;  for(intS0= (S-1) &S; S0; S0= (s0-1) &S) {intS1 = SS0; if(area[s0]%x==0&& dfs (S0, min (x, area[s0]/x)) && dfs (S1, min (x, area[s1]/x )))returndp[s][x]=1; if(area[s0]%y==0&& dfs (S0, min (y, area[s0]/y)) && dfs (S1, min (Y, area[s1]/y )))returndp[s][x]=1; }    returndp[s][x]=0;}intMain () {intn, x, Y, S, kase=1;  while(SCANF ("%d", &n)! = EOF &&N) {scanf ("%d%d", &x, &X); S=0;  for(intI=0; i<n; i++) {scanf ("%d", &A[i]); S+=A[i]; }        intAll = (1<<n)-1;  for(intI=0; i<=all; i++) {Area[i]=0; inttemp = i, cur=0;  while(temp) {if(temp&1) Area[i] + =A[cur]; Cur++; Temp>>=1; }} memset (DP,-1,sizeof(DP)); printf ("Case %d:%s\n", kase++, S = = X*y && dfs (all, min (x, y))?"Yes":"No"); }        return 0;}

Uvalive-4794--sharing Chocolate

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.