标签:
hashmap判重大法好
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 #define MOD 1000000007 10 const int INF=0x3f3f3f3f; 11 const double eps=1e-5; 12 typedef long long ll; 13 #define cl(a) memset(a,0,sizeof(a)) 14 #define ts printf("*****\n"); 15 const int MAXN=1005; 16 int n,m,tt; 17 int sum=0,a[MAXN]; 18 const int HASH = 1000007; 19 struct HASHMAP 20 { 21 int head[HASH],next[HASH],size; 22 int state[HASH][3]; 23 int hash(int a,int b,int c) 24 { 25 return (((a*7+b)*7+c)*7)%HASH; //131 26 } 27 void init() 28 { 29 size = 0; 30 memset(head,-1,sizeof(head)); 31 } 32 int insert(int a,int b,int c) 33 { 34 int h = hash(a,b,c); 35 for(int i = head[h]; i != -1;i = next[i]) 36 if(a == state[i][0]&&b == state[i][1]&&c == state[i][2]) 37 { 38 return 1; 39 } 40 state[size][0]=a,state[size][1]=b,state[size][2]=c; 41 next[size] = head[h]; 42 head[h] = size++; 43 return 0; 44 } 45 } H1; 46 bool check(int a1,int a2,int a3) 47 { 48 if(a1+a2>a3&&a1+a3>a2&&a2+a3>a1) return 1; 49 else return 0; 50 } 51 void dfs(int a1,int a2,int a3,int pos) 52 { 53 if(a3>sum/3) return; 54 if(pos>=n) 55 { 56 if(a1==0||a2==0||a3==0) return; 57 if(check(a1,a2,a3)&&a1>=a2&&a2>=a3) 58 { 59 H1.insert(a1,a2,a3); 60 } 61 return; 62 } 63 dfs(a1+a[pos],a2,a3,pos+1); 64 dfs(a1,a2+a[pos],a3,pos+1); 65 dfs(a1,a2,a3+a[pos],pos+1); 66 } 67 68 int main() 69 { 70 int i,j,k,ca=1; 71 #ifndef ONLINE_JUDGE 72 freopen("1.in","r",stdin); 73 #endif 74 scanf("%d",&tt); 75 while(tt--) 76 { 77 H1.init(); 78 sum=0; 79 scanf("%d",&n); 80 for(i=0;i<n;i++) scanf("%d",a+i),sum+=a[i]; 81 dfs(0,0,0,0); 82 printf("%d\n",H1.size); 83 } 84 }
hdu 4277 2012长春赛区网络赛 dfs+hashmap ***
标签:
原文地址:http://www.cnblogs.com/cnblogs321114287/p/4732753.html