Sharing Chocolate - UVa 1099 状压dp

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

题意:问x*y的巧克力能否分成给定的n块大小。

思路:dp[c][S]表示状压状态为S的巧克力能否以c为短边组成矩形,然后dfs。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int sum[100010],num[20],vis[110][100010],dp[110][100010],pow2[20];
int n,t;
int dfs(int c,int S)
{
    if(vis[c][S]==t)
      return dp[c][S];
    int i,j,k,l,ans=0,S1,S2;
    vis[c][S]=t;
    for(j=0;j<n;j++)
       if(S==pow2[j])
          return dp[c][S]=1;
    l=sum[S]/c;
    for(S1=(S-1)&S;S1>0;S1=(S1-1)&S)
    {
        S2=S-S1;
        if(sum[S1]%c==0 && dfs(min(c,sum[S1]/c),S1) && dfs(min(c,sum[S2]/c),S2))
          return dp[c][S]=1;
        if(sum[S1]%l==0 && dfs(min(l,sum[S1]/l),S1) && dfs(min(l,sum[S2]/l),S2))
          return dp[c][S]=1;
    }
    return dp[c][S]=0;
}
int main()
{
    int i,j,k,c,l,S,ans;
    pow2[0]=1;
    for(i=1;i<=20;i++)
       pow2[i]=pow2[i-1]*2;
    while(~scanf("%d",&n) && n>0)
    {
        t++;
        scanf("%d%d",&c,&l);
        for(i=0;i<n;i++)
           scanf("%d",&num[i]);
        S=pow2[n]-1;
        for(i=1;i<=S;i++)
        {
            sum[i]=0;
            for(j=0;j<n;j++)
               if(i&pow2[j])
                 sum[i]+=num[j];
        }
        if(c*l!=sum[S])
          ans=0;
        else
          ans=dfs(min(c,l),S);
        printf("Case %d: ",t);
        if(ans)
          printf("Yes\n");
        else
          printf("No\n");
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: sharing-jdbc是一个基于Java的开源数据库中间件,提供了对数据库连接池、数据库读写分离、分库分表等功能的支持。使用sharing-jdbc可以简化我们对数据库的操作,提高系统的性能和可扩展性。 MyBatis是一个持久层框架,它提供了对数据库的操作方法和SQL语句的映射。在使用MyBatis时,我们可以配置数据源和数据库连接信息。将sharing-jdbc与MyBatis结合使用,可以实现对数据库的读写分离和分库分表功能。 Spring Boot是一个快速开发Spring应用的框架,它提供了简化配置和集成的功能。在Spring Boot应用中,我们可以使用YAML文件(.yml)来配置应用的各种参数和组件。通过在YAML配置文件中添加相应的配置信息,可以实现sharing-jdbc、MyBatis和Spring Boot的集成和配置。 在YAML配置文件中,我们需要配置数据库连接信息、数据源、MyBatis的相关配置和sharing-jdbc的相关配置。例如,我们可以配置数据库的URL、用户名和密码,指定MyBatis的映射文件路径和配置文件路径,以及指定sharing-jdbc的相关信息。 配置sharing-jdbc的方式如下: ```yaml sharing-jdbc: dataSources: - name: dataSource1 dataSourceClassName: com.mysql.jdbc.Driver jdbcUrl: jdbc:mysql://localhost:3306/db1 username: root password: password1 shardingRuleConfigs: - defaultDataSourceName: dataSource1 defaultDatabaseStrategy: inline: algorithmExpression: ds_${user_id % 2} defaultTableStrategy: standard: algorithmExpression: t_${user_id % 16} bindingTables: - table_patterns: user_${0..15} actualDataNodes: dataSource1.user_${0..15} ``` 在以上示例中,我们配置了一个数据库连接池(dataSource1),并指定了数据源的相关信息。然后,我们配置了分库分表的规则,根据用户ID分配数据库和数据表,并将其与数据源绑定。 通过这样的配置,我们可以在Spring Boot应用中方便地使用sharing-jdbc和MyBatis操作数据库,并利用分库分表功能提高应用的性能和扩展性。 ### 回答2: 在使用Spring Boot中整合Sharing-JDBC和MyBatis时,我们可以使用yml配置文件来配置相关信息。 首先,我们需要在yml配置文件中设置Sharing-JDBC的数据源配置。具体配置内容如下: ``` # Sharing-JDBC 数据源配置 sharing: shardingsphere: datasource: name: # 数据源名称 url: # 数据库连接地址 username: # 数据库用户名 password: # 数据库密码 driver-class-name: # 数据库驱动类名 ``` 其中,name表示数据源的名称,url表示数据库连接地址,username表示数据库用户名,password表示数据库密码,driver-class-name表示数据库驱动类名。 接下来,我们需要配置MyBatis的相关信息。具体配置内容如下: ``` # MyBatis配置 mybatis: mapper-locations: classpath:mapper/*.xml # MyBatis Mapper文件的存放位置 type-aliases-package: com.example.model # 实体类的包名 ``` 其中,mapper-locations表示Mapper文件的存放位置,type-aliases-package表示实体类的包名。 然后,我们需要在Spring Boot的主配置类中使用@MapperScan注解来扫描Mapper接口所在的包,并将其注册为Spring Bean。具体配置内容如下: ```java @Configuration @MapperScan("com.example.mapper") // Mapper接口所在的包 public class MybatisConfig { } ``` 在以上配置完成之后,我们可以在项目中使用Sharing-JDBC和MyBatis进行数据库访问。比如,创建一个Mapper接口和对应的Mapper XML文件,进行SQL的编写和数据库操作。 以上就是使用Sharing-JDBC、MyBatis和Spring Boot整合的yml配置方式。通过配置yml文件,我们可以方便地配置数据源、MyBatis等相关信息,简化项目的配置和管理。 ### 回答3: sharing-jdbc是一个基于Java的数据库中间件,可以方便地支持数据库的分库分表操作。而MyBatis是一个优秀的持久层框架,与数据库之间的关系映射由它负责。Spring Boot是一个快速开发框架,可以简化Java应用程序的配置和部署。 在Spring Boot中,可以使用YAML(又称为YML)文件来进行配置。YAML是一种人类可读的数据序列化格式,与JSON类似,但语法更加简洁。使用YAML配置文件,可以将各种配置信息以键值对的形式存储在一个文件中。 当使用sharing-jdbc、MyBatis和Spring Boot一起使用时,我们需要在YAML配置文件中进行相应的配置。首先,需要配置数据库连接信息,包括数据库URL、用户名和密码等。其次,需要配置MyBatis的相关信息,如Mapper的路径、扫描的包等。最后,还需要配置sharing-jdbc的相关信息,包括分库分表的策略、数据库的数据源等。 下面是一个示例的YAML配置文件: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 mybatis: mapper-locations: classpath:mapper/*.xml sharing-jdbc: data-sources: ds_0: jdbc-url: jdbc:mysql://localhost:3306/db_0?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 ds_1: jdbc-url: jdbc:mysql://localhost:3306/db_1?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 sharding-rule-config-file: classpath:sharding-jdbc.yml ``` 在这个示例中,我们配置了一个主数据库(test)和两个分库(db_0和db_1)。分别使用了不同的数据源(ds_0和ds_1)进行连接。同时,我们还指定了MyBatis的Mapper文件路径和sharing-jdbc的分库分表策略文件路径。 通过这样的配置,我们可以在Spring Boot应用中方便地使用sharing-jdbc和MyBatis来实现数据库的分库分表操作。这种配置方式简洁明了,提高了开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值