题目

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. 我的页面定制CSS代码(SimpleGamboge皮肤)

    我的页面定制CSS代码,针对博客园SimpleGamboge皮肤. 调整: 1.左上图片更换为自己的头像 2.扩大左侧栏宽度,缩小右侧主栏宽度宽度 3.扩大内容页面的评论区宽度,工具图标靠左 4.去广 ...

  2. EBS中加载FORM使用的JavaBean的JAR包

    1.SSH端,APP用户登录: 2.输入env,确定环境变量名称FORMS_WEB_CONFIG_FILE: 3.定位模板文件:     $AD_TOP/bin/adtmplreport.sh con ...

  3. easyUI中onLoadSuccess方法获取多个对象

    后台将一个list中的对象循环放到map中,如下: @RequestMapping("/getSaleOrderDetailList") @ResponseBody public ...

  4. linux 命令02

    cp 文件 路径 文件拷贝 cp -a 目录 路径 拷贝目录 cd .. 回到上一级目录 mv 需要移动目录(文件) 移动目的地 移动目录(文件) pwd 查看用户当前所在路径 ls 查看目录列表 m ...

  5. MySQL主从复制

    Mysql主从复制介绍 MySQL支持单向.双向.链式级联.实时.异步复制.在复制过程中,一台服务器充当服务器(Master),而一个或多个其它的服务器充当从服务器(Slave). 复制可以是单向:M ...

  6. git用法之[回滚代码]

    我们在写代码的任何过程中,都有可能出错,任何过程都有可能要!回!滚!代!码!事关重大!一定要详细讲讲. 一.关于 工作区.暂存区.本地分支: 工作区:即自己当前分支所修改的代码,git add xx ...

  7. c++虚析构函数

    虚析构函数的作用主要是当通过基类指针删除派生类对象时,调用派生类的析构函数(如果没有将不会调用派生类析构函数) #include <iostream> using namespace st ...

  8. Spring框架学习(一)

    一. spring概述 Spring 框架是一个分层架构,由 7 个定义良好的模块组成.Spring 模块构建在核心容器之上,核心容器定义了创建.配置和管理 bean 的方式,如图 1 所示. 图 1 ...

  9. sql server2008根据经纬度计算两点之间的距离

    --通过经纬度计算两点之间的距离 create FUNCTION [dbo].[fnGetDistanceNew] --LatBegin 开始经度 --LngBegin 开始维度 --29.49029 ...

  10. UFLDL 教程三总结与答案

    主成分分析(PCA)是一种能够极大提升无监督特征学习速度的数据降维算法.更重要的是,理解PCA算法,对实现白化算法有很大的帮助,很多算法都先用白化算法作预处理步骤.这里以处理自然图像为例作解释. 1. ...