hdu 4277 USACO ORZ (dfs暴搜+hash)
题目大意:有N个木棒,相互组合拼接,能组成多少种不同的三角形。
思路:假设c>=b>=a 然后枚举C,在C的dfs里嵌套枚举B的DFS。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define mod 2000007
using namespace std; int n;
int X[20];
bool vis[20];
int ans;
long long sum; long long hash[mod]; bool ok(int b,int c)
{
int a=sum-b-c;
if(a+b>c && c>=b && b>=a)return true; return false;
} bool work(long long t)
{
int v=t%mod; while(hash[v]!=t && hash[v]!=-1)//.....之前这里写成了IF 卡了半天再找到。以后要细心。。。
v=(v+10)%mod; if(hash[v]==-1)
{
hash[v]=t;
return true;
}
return false;
} void dfsb(int pos,int c,int b)
{
//printf("c = %d b = %d\n",c,b);
for(int i=pos;i<=n;i++)
{
if(vis[i])continue; vis[i]=true;
b+=X[i]; if(ok(b,c))
{
//printf("c = %d,b = %d,a = %d\n",c,b,sum-b-c);
int a=sum-b-c;
long long t=(long long)a+(long long)b*sum+(long long)c*sum*sum;
if(work(t))ans++;
}
if(b<=c)dfsb(i,c,b); b-=X[i];
vis[i]=false;
}
} void dfsc(int pos,int c)
{
for(int i=pos;i<=n;i++)
{
if(vis[i])continue; vis[i]=true;
c+=X[i]; if(c>=sum/3 && c<=sum/2)dfsb(1,c,0);//剪枝 dfsc(i,c); c-=X[i];
vis[i]=false;
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n); memset(vis,0,sizeof(vis));
memset(hash,-1,sizeof(hash)); sum=0; for(int i=1;i<=n;i++)
{
scanf("%d",&X[i]);
sum+=X[i];
}
ans=0;
dfsc(1,0);
printf("%d\n",ans);
}
return 0;
}
hdu 4277 USACO ORZ (dfs暴搜+hash)的更多相关文章
- hdu 4277 USACO ORZ dfs+hash
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...
- hdu 4277 USACO ORZ DFS
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 4277 USACO ORZ(DFS暴搜+set去重)
原题代号:HDU 4277 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4277 原题描述: USACO ORZ Time Limit: 5000/1 ...
- HDU 4277 USACO ORZ(暴力+双向枚举)
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 4277 USACO ORZ(dfs+剪枝)
Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pasture ...
- hdu 4277 USACO ORZ (Dfs)
题意: 给你n个数,要你用光所有数字组成一个三角形,问能组成多少种不同的三角形 时间分析: 3^15左右 #include<stdio.h> #include<set> usi ...
- hdu 4277 USACO ORZ
没什么好方法,只能用dfs了. 代码如下: #include<iostream> #include<cstring> #include<cstdio> #inclu ...
- HDU 4284 Travel (Folyd预处理+dfs暴搜)
题意:给你一些N个点,M条边,走每条边要花费金钱,然后给出其中必须访问的点,在这些点可以打工,但是需要先拿到证书,只可以打一次,也可以选择不打工之直接经过它.一个人从1号点出发,给出初始金钱,问你能不 ...
- Usaco 2.3 Zero Sums(回溯DFS)--暴搜
Zero SumConsider the sequence of digits from 1 through N (where N=9) in increasing order: 1 2 3 ... ...
随机推荐
- Equals 和==
class Person { private string name; public string Name { get ...
- 【ThinkingInC++】66、pointer Stash的使用
头文件PStash.h /** * 书本:[ThinkingInC++] * 功能:pointer Stash的头文件 * 时间:2014年10月5日14:33:15 * 作者:cutter_poin ...
- white-space的值
white-space的值:normal 默认.空白会被浏览器忽略.pre 空白会被浏览器保留.其行为方式类似 HTML 中的 标签.nowrap 文本不会换行,文本会在在同一行上继续,直到遇到 标签 ...
- MVC 分页1 标准的url分页
一. 将mvcpager ddl 引用到web服务项目中. 二. 在view加入 <%@ Import Namespace="Webdiyer.WebControls.Mvc" ...
- Swift初探一
今天安装了一下Xcode6-Beta版,想来体验一下Swift的魅力:安装Swift系统最低版本号为:10.9.3 仅仅看看一点The Swift Programming Language,以下给大家 ...
- leetcod Pow(x, n)
题目:就是实现一个指数函数. 直接用一个while一直乘以n词肯定是会超时的. 自己写了用递归(而且是很挫的递归),测试了无数次,根据每个case去修改代码.终于可以AC了.不忍直视,自己写了好长,如 ...
- PHP专业开发IDE——Zend Studio 10.5预览版发布
Zend Studio是新一代的PHP IDE,高效的开发和维护PHP代码是它的核心.Zend公司目前已发布了Zend Studio 10.5预览版,预览版中提高了快速响应能力和时时误差检查.因此使用 ...
- CSS移动
#hand { width: 170px; height: 236px; position: absolute; top: 178px; left: 390px; background: url('h ...
- mysql的架构
和其他数据库相比,mysql有点与众不同,它的架构可以在多种不同场景中应用并发挥好的作用,而理解其设计是发挥好作用的先决条件 每当我们在想起mysql的逻辑架构师,我们可以构造一副mysql各组件之间 ...
- xheditor 进阶
xhEditor提供两种方式初始化编辑器: 方法1:利用class属性来初始化和传递各种初始化参数,例: class="xheditor {skin:'default'}" 方法 ...