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