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 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.)

\epsfbox{p4794.eps}

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$ \le$n$ \le$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$ \le$xy$ \le$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 

4 
3 4 
6 3 2 1 
2 
2 3 
1 5 
0

Sample Output 

Case 1: Yes 
Case 2: No

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
const int MAXN = 16;
int n, x, y;
int a[MAXN];
int sum[1<<MAXN];
int dp[110][1<<MAXN]; // 1 为可以 0为不可以 -1为未确定
int bitcount(int a)
{
    int rt = 0;
    while(a)
    {
        if(a&1) rt++;
        a>>=1;
    }
    return rt;
}
int dfs(int x,int s)
{
    if(dp[x][s] != -1)return dp[x][s];
    if(bitcount(s)==1)return dp[x][s] = 1;
    int y = sum[s]/x;
    for(int s0=(s-1)&s; s0; s0=(s0-1)&s)
    {
        int s1 = s^s0;
        //一刀切完之后总有一个不变的
        if(sum[s0]%x==0 && dfs(min(x,sum[s0]/x),s0) && dfs(min(x,sum[s1]/x),s1) )
            return dp[x][s] = 1;
        if(sum[s0]%y==0 && dfs(min(y,sum[s0]/y),s0) && dfs(min(y,sum[s1]/y),s1) )
            return dp[x][s] = 1;
    }
    return dp[x][s] = 0;
}
int main()
{
    int cs = 1;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)break;
        scanf("%d%d",&x,&y);
        int ct = 0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            ct += a[i];
        }
        if(ct!=x*y || ct%x!=0)
        {
            printf("Case %d: No\n",cs++); continue;
        }
        int ALL = (1<<n) - 1;
        for(int s=0; s<=ALL; s++)
        {
            sum[s] = 0;
            for(int i=0; i<n; i++)
            {
                if(s&(1<<i))
                {
                    sum[s] += a[i];
                }
            }
        }
        memset(dp,-1,sizeof dp);
        if(dfs(min(x,y),ALL)==1)
        {
            printf("Case %d: Yes\n",cs++);
        }else{
            printf("Case %d: No\n",cs++);
        }
    }
    return 0;
}


您可能感兴趣的与本文相关的镜像

Dify

Dify

AI应用
Agent编排

Dify 是一款开源的大语言模型(LLM)应用开发平台,它结合了 后端即服务(Backend as a Service) 和LLMOps 的理念,让开发者能快速、高效地构建和部署生产级的生成式AI应用。 它提供了包含模型兼容支持、Prompt 编排界面、RAG 引擎、Agent 框架、工作流编排等核心技术栈,并且提供了易用的界面和API,让技术和非技术人员都能参与到AI应用的开发过程中

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值