题目

Source

http://acm.hdu.edu.cn/showproblem.php?pid=5937

Description

Little Ruins is a studious boy, recently he learned addition operation! He was rewarded some number bricks of 1 to 9 and infinity bricks of addition mark '+' and equal mark '='.

Now little Ruins is puzzled by those bricks because he wants to put those bricks into as many different addition equations form x+y=z as possible. Each brick can be used at most once and x, y, z are one digit integer.

As Ruins is a beginer of addition operation, x, y and z will be single digit number.

Two addition equations are different if any number of x, y and z is different.

Please help little Ruins to calculate the maximum number of different addition equations.

Input

First line contains an integer T, which indicates the number of test cases.

Every test case contains one line with nine integers, the ith integer indicates the number of bricks of i.

Limits
1≤T≤30
0≤bricks number of each type≤100

Output

For every test case, you should output 'Case #x: y', where x indicates the case number and counts from 1 and y is the result.

Sample Input

3
1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
0 3 3 0 3 0 0 0 0

Sample Output

Case #1: 2
Case #2: 6
Case #3: 2

分析

题目大概说有若干个1到9这几个数字问最多能拼成多少种x+y=z的等式?

  • x+y=z有36种。由于x>y和x<y是对称的,只考虑x<=y,有20种,16种是x<y,4种x=y。。
  • 直接暴力搜索。。对于x<y,可以选1种、选2种和不选;对于x=y可以选和不选。
  • 那么这样时间复杂度是$O(3^{16}*2^4)$。。
  • 不剪枝会超时的,我加了几个预测的最优性剪枝,利用剩下的各个数字的个数粗略估算最多还能加入几种等式。。
  • 实测100 100 100 100 100 100 100 100 100 100秒出= =。。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int x[]={1,1,1,1,1,1,1,2,2,2,2,2,3,3,3,4};
int y[]={2,3,4,5,6,7,8,3,4,5,6,7,4,5,6,5};
int z[]={3,4,5,6,7,8,9,5,6,7,8,9,7,8,9,9}; int x2[]={1,2,3,4};
int y2[]={2,4,6,8}; int a[11],ans;
void dfs2(int k,int n){
if(ans<n) ans=n;
if(k==4) return;
if(a[x2[k]]>1 && a[y2[k]]){
a[x2[k]]-=2; --a[y2[k]];
dfs2(k+1,n+1);
a[x2[k]]+=2; ++a[y2[k]];
}
dfs2(k+1,n);
}
inline calc(int a,int b){
if(a<b) return a<<1;
return b<<1;
}
void dfs(int k,int n){
if(k<=7){
if(n+4+calc(a[1],7-k)+calc(a[2],5)+calc(a[3],3)+calc(a[4],1)<ans) return;
}else if(k<=12){
if(n+4+calc(a[2],12-k)+calc(a[3],3)+calc(a[4],1)<ans) return;
}else{
if(n+4+calc(a[3],15-k)+calc(a[4],1)<ans) return;
}
if(k==16){
dfs2(0,n);
return;
}
dfs(k+1,n);
if(((x[k]==y[k]&&a[x[k]]>1) || (x[k]!=y[k]&&a[x[k]])) && a[y[k]] && a[z[k]]){
--a[x[k]]; --a[y[k]]; --a[z[k]];
dfs(k+1,n+1);
++a[x[k]]; ++a[y[k]]; ++a[z[k]];
}
if(x[k]!=y[k] && a[x[k]]>1 && a[y[k]]>1 && a[z[k]]>1){
a[x[k]]-=2; a[y[k]]-=2; a[z[k]]-=2;
dfs(k+1,n+2);
a[x[k]]+=2; a[y[k]]+=2; a[z[k]]+=2;
}
} int main(){
int t;
scanf("%d",&t);
for(int cse=1; cse<=t; ++cse){
for(int i=1; i<=9; ++i){
scanf("%d",a+i);
}
ans=0;
dfs(0,0);
printf("Case #%d: %d\n",cse,ans);
}
return 0;
}

HDU5937 Equation(DFS + 剪枝)的更多相关文章

  1. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  2. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  3. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  4. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  5. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  6. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  7. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. LA 6476 Outpost Navigation (DFS+剪枝)

    题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...

  9. poj 1011 Sticks (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Descrip ...

  10. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

随机推荐

  1. Eclipse常用快捷键汇总

    经常使用eclipse进行开发,不掌握快捷键步行啊,在此整理了一些快捷键,大家要灵活运用啊... (注:红色标出来的是经常使用到的快捷键,磨刀不误砍柴工啊...) Ctrl+1 快速修复(最经典的快捷 ...

  2. python note

    =和C一样,为赋值.==为判断,等于.但是,在python中是不支持行内赋值的,所以,这样避免了在判断的时候少写一个出错. dictionary 的key唯一,值可以为很多类型. list的exten ...

  3. ubuntu配置ftp服务器

    sudo apt-get update sudo apt-get install vsftpd sudo vi /etc/vsftpd.conf listen=YES anonymous_enable ...

  4. 面向对象Part1对象的创建和Static!

    面向对象的特征: 1)封装 2)继承 3)多台 4)抽象 创建的是什么类型的对象 变量的声明就是什么类型. class Servant{ void xxx (){} } Servant s1 = ne ...

  5. 卸载linux Mint自带jdk并安装最新jdk

    查看安装的软件包sudo dpkg --list | grep -i jdk 删除jdksudo apt-get purge openjdk* 删除其他的包sudo apt-get purge ice ...

  6. mysql 性能优化方向

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  7. c#延迟加载

    public class BlogUser { public int Id { get; private set; } public Lazy<List<Article>> A ...

  8. WPF去边框与webbrowser的冲突

    首先建一个类,比如NativeMethods.cs class NativeMethods{     public const int WS_CAPTION=0x00C0000;     public ...

  9. Echarts 3.19 制作常用的图形 非静态

    最近阿里内部使用的 图表也向外开放了 而百度就好像更有良心一点,Echarts 早就开放了 . 自己学Echarts的时候走了很多的弯路,毕竟谁让自己菜呢,多撞几次南墙才晓得疼 才知道学习方法,新手上 ...

  10. SqlServer 2008登录时报错

    登录SQLServer2008R2时提示如下错误: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server ...