http://acm.hdu.edu.cn/showproblem.php?pid=1518
#include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> using namespace std; const int dx[4]= {1,0,-1,0}; const int dy[4]= {0,1,0,-1}; int a[105]; int ans,n,ave; int vis[105]; bool cmp(int a,int b) { return b>a; } int dfs(int s,int x,int cnt) { if(s==ave) { s=0; x=0; cnt++; if(cnt==4) { return 1; } } for(int i=x; i<n; i++) { if(!vis[i]) { vis[i]=1; if((s+a[i]<=ave)&&dfs(s+a[i],i+1,cnt)) { return 1; } vis[i]=0; } } return 0; } int main() { int T; scanf("%d",&T); while(T--) { memset(vis,0,sizeof(vis)); scanf("%d",&n); int sum=0; for(int i=0; i<n; i++) { scanf("%d",&a[i]); sum+=a[i]; } int ans=0; if(sum%4==0) { ave=sum/4; ans=dfs(0,0,0); } if(ans) { printf("yes "); } else { printf("no "); } } return 0; }