LA4794 分享巧克力

Sharing Chocolate

Chocolate in its many forms is enjoyed by millions of people around the world every day. It is a truly universal candy available in virtually every country around the world.

You find that the only thing better than eating chocolate is to share it with friends. Unfortunately your friends are very picky and have different appetites: some would like more and others less of the chocolate that you offer them. You have found it increasingly difficult to determine whether their demands can be met. It is time to writte a program that 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 you may break one bar into two pieces along a division between rows or columns of the bar. You or the may then repeatedly break the resulting pieces in the same manner. Each of your friends insists on a getting a single rectangular portion of the chocolate that has a specified number of pieces. You are a little bit insistent as well: you will break up your bar only if all of it can be distributed to your friends, with none left over.

For exampla, Figure 9 shows one way that a chocolate bar consisting of 3 x 4 pieces can be split into 4 parts that 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 integer n (1<=n<=15), the number of parts in which the bar is supposed to be split. This is followed by a line containing two integers x and y(1<=xy<=100), the dimensions of the chocolate bar. The next line contains n positive integers, giving the number of pieces that are supposed to be in each of the n parts.

The input is terminated by a line containing the integer zero.

Output

For each test case, first display its case number. Then display whether it is possible to break the chocolate in the desired way: display ``Yes" if it is possible, and ``No" otherwise. Follow the format of the sample output.

Sample Input


3 4 
6 3 2 1 

2 3 
1 5 0

Sample Output

Case 1: Yes
Case 2: No 题意: 将一个x*y大小的巧克力分成n分, 固定大小a[i], 判断是否可以成功. 解题思路:
1. 设状态dp[x][y][S]: x*y的巧克力是否可以分割成集合S.
2. dp[x][y][S]=true 当且仅当:
  dp[x0][y][S0] = dp[x-x0][y][S-S0] = true //1<=x0<x,S0是S的子集
OR dp[x][y0][S0] = dp[x][y-y0][S-S0] = true //1<=y0<y,S0是S的子集
3.把集合S中面积的总和记为sum[S]
4.只计算sum(S)=x*y的
5.dp[x][y][S] = dp[y][x][S],不必重复计算
我们设x<y,用dp[x][S]代替dp[x][y][S]和dp[y][x][S]
此时x为较小的边长,y为sum(S)/x.
方程变化为:
dp[x][S]=true 当且仅当:   
    sum[S0]%x==0 AND dp[min(x,sum[S]/x)][S0] = dp[min(x,sum[S]/x)][S-S0] = true
或者是 sum[S0]%y==0 AND dp[min(y,sum[S]/y)][S0] = dp[min(y,sum[S]/y)][S-S0] = true
6.输入之后要比较所有所有a[i]的和是否等于x*y

代码如下:
#include<iostream>
#include<cstring>
#define Max_n 15
#define Size 105
using namespace std; bool d[Size][(<<Max_n)];
bool vis[Size][(<<Max_n)];
int sum[(<<Max_n)];
int a[Max_n];
int n,x,y; int bitcount(int S){
int num=;
for(int i=;i<n;i++)
if(S&(<<i))num++;
return num;
} bool dp(int x,int S){
if(vis[x][S])return d[x][S]; vis[x][S]=true;
bool& ans=d[x][S]; if(bitcount(S)==)return ans=true; int y=sum[S]/x;
for(int S0=(S-)&S;S0;S0=(S0-)&S){
int S1=S-S0;
if(sum[S0]%x== && dp(min(x,sum[S0]/x),S0) && dp(min(x,sum[S1]/x),S1)) return ans=true;
if(sum[S0]%y== && dp(min(y,sum[S0]/y),S0) && dp(min(y,sum[S1]/y),S1)) return ans=true;
} return ans=false;
} int main(){
freopen("32.in","r",stdin); while(scanf("%d",&n)== && n!=){
memset(vis,false,sizeof(vis)); cin>>x>>y;
for(int i=;i<n;i++){
cin>>a[i];
} for(int S=;S<(<<n);S++){
for(int j=;j<n;j++){
if(S&(<<j)){
sum[S]+=a[j];
}
}
} int ALL=(<<n)-;
int ans;
if(sum[ALL]!=x*y)ans=;
else ans=dp(min(x,y),ALL); cout<<(ans?"Yes":"No")<<endl;
} fclose(stdin);
return ;
}
上一篇:appium(12)-The starting of an app


下一篇:Linux学习-软件磁盘阵列 (Software RAID)