hdu 4277 2012长春赛区网络赛 dfs+hashmap ***
2015-08-15 17:02
501 查看
hashmap判重大法好
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<queue> #include<map> using namespace std; #define MOD 1000000007 const int INF=0x3f3f3f3f; const double eps=1e-5; typedef long long ll; #define cl(a) memset(a,0,sizeof(a)) #define ts printf("*****\n"); const int MAXN=1005; int n,m,tt; int sum=0,a[MAXN]; const int HASH = 1000007; struct HASHMAP { int head[HASH],next[HASH],size; int state[HASH][3]; int hash(int a,int b,int c) { return (((a*7+b)*7+c)*7)%HASH; //131 } void init() { size = 0; memset(head,-1,sizeof(head)); } int insert(int a,int b,int c) { int h = hash(a,b,c); for(int i = head[h]; i != -1;i = next[i]) if(a == state[i][0]&&b == state[i][1]&&c == state[i][2]) { return 1; } state[size][0]=a,state[size][1]=b,state[size][2]=c; next[size] = head[h]; head[h] = size++; return 0; } } H1; bool check(int a1,int a2,int a3) { if(a1+a2>a3&&a1+a3>a2&&a2+a3>a1) return 1; else return 0; } void dfs(int a1,int a2,int a3,int pos) { if(a3>sum/3) return; if(pos>=n) { if(a1==0||a2==0||a3==0) return; if(check(a1,a2,a3)&&a1>=a2&&a2>=a3) { H1.insert(a1,a2,a3); } return; } dfs(a1+a[pos],a2,a3,pos+1); dfs(a1,a2+a[pos],a3,pos+1); dfs(a1,a2,a3+a[pos],pos+1); } int main() { int i,j,k,ca=1; #ifndef ONLINE_JUDGE freopen("1.in","r",stdin); #endif scanf("%d",&tt); while(tt--) { H1.init(); sum=0; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",a+i),sum+=a[i]; dfs(0,0,0,0); printf("%d\n",H1.size); } }
相关文章推荐
- High Performance Browser Networking - TCP UDP TLS
- 一起学CC3200之HttpServer文件传输(中序)
- cf-546E 网络流,状态转移平衡
- poj 2112 网络流+二分
- IOS--UI--HTTP/总结
- ECG信号读出,检测QRS,P,T 波(小波去噪,并根据检测),基于BP辨识的神经网络
- LINUX C 的TCP发送接收测试
- TCP/IP的经典网络编程
- HTTP 状态消息
- 浅议iOS网络数据解析
- Volley网络框架完全解析(实战篇)
- Volley网络框架完全解析(实战篇)
- hdu 4273 2012长春赛区网络赛 三维凸包中心到最近面距离 ***
- hdu 4272 2012长春赛区网络赛 dfs暴力 ***
- Android传统HTTP请求get----post方式提交数据(包含乱码问题)
- 大型网络游戏服务器的框架设计
- Ubuntu/centos网络接口没有eth0但是有eth1或eth2的问题
- http 自己组装格式
- 小言HTTP Authentication
- http请求获取到的网页源码是字节数组还是字符串?