Sharing chocolate

目要求只能横着切和纵着切,而且必须一次切到底,所以每次切割的巧克力只能是矩形,设l w分别为举行的的宽和长,S表示要把巧克力切割成块的集合,sum[S]表示其面积的和,只有l*w == sum[S]的状态才是有可能切割成功的,所以可以定义状态table[l][S]表示宽为l的矩形切割成状态为S的块是否可以成功,因为S和l确定了,w也就确定了,其中 w = sum[S]/l , table[l][S] =  ∑(table[min(l, sum[S0]/l)][S0]&&table[min(l, sum[S0]/l)][S-S0] || table[min(w, sum[S0]/w)][S0]&&table[min(w, sum[S0]/w)][S-S0])∑为逻辑加


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits> 
#include <set>
#include <string> 
#include <sstream>
#include <utility>
#include <ctime>
//#pragma comment(linker, "/STACK:1024000000,1024000000") 

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::getline;
using std::make_pair;
using std::greater;

const int MAXN(15);
int table[110][(1 << MAXN)+10];
int sum[(1 << MAXN)+10];

int dfs(int l, int S)
{
	if(table[l][S] != -1)
		return table[l][S];
	int &cur = table[l][S];
	if((S^(S&(-S))) == 0)
		return cur = 1;
	int w = sum[S]/l;
	for(int S0 = (S-1)&S; S0; S0 = (S0-1)&S)
	{
		int S1 = S0^S;
		if(sum[S0]%l == 0 && dfs(min(l, sum[S0]/l), S0) && dfs(min(l, sum[S1]/l), S1))
			return cur = 1;
		if(sum[S0]%w == 0 && dfs(min(w, sum[S0]/w), S0) && dfs(min(w, sum[S1]/w), S1))
			return cur = 1;
	}
	return cur = 0;
}

int main()
{
	int N, n_case(0);
	int x, y;
	while(scanf("%d", &N), N)
	{
		scanf("%d%d", &x, &y);
		for(int i = 0; i < N; ++i)
		{
			int temp;
			scanf("%d", &temp);
			sum[1 << i] = temp; 
		}
		int limit = (1 << N)-1;
		for(int i = 1; i <= limit; ++i)
		{
			sum[i] = sum[i^(i&(-i))]+sum[i&(-i)];
		}
		int ans = 0;
		if(sum[limit] == x*y)
		{
			memset(table, -1, sizeof(table));
			ans = dfs(min(x, y), limit);
		}
		printf("Case %d: %s\n", ++n_case, ans? "Yes": "No");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值