-
C - Sum It Up
- HDU - 1258
- 题意:
- 从所给序列中挑出一些能够得到和为sum的和式注意去重dfs即可
-
#include<bits/stdc++.h> using namespace std; #define maxn 1555 long long sum,n,a[maxn],flag,vis[maxn],b[maxn]; map<vector<int>,bool>mmp; bool cmp(int x,int y) { return x>y; } void dfs(int s,int cnt) { if(s>sum) return; if(s==sum) { vector<int>q; for(int i=0; i<cnt; i++) q.push_back(b[i]); sort(q.begin(),q.end()); if(mmp[q]==0) { cout<<b[0]; for(int i=1; i<cnt; i++) { cout<<"+"<<b[i]; } cout<<endl; mmp[q]=1; } flag=0; return; } for(int j=0; j<n; j++) if(!vis[j]) { vis[j]=1; b[cnt]=a[j]; dfs(s+a[j],cnt+1); vis[j]=0; } } int main() { while(cin>>sum>>n) { memset(vis,0,sizeof(vis)); mmp.clear(); flag=1; if(sum==0&&n==0) break; for(int i=0; i<n; i++) cin>>a[i]; sort(a,a+n,cmp); cout<<"Sums of "<<sum<<":"<<endl; dfs(0,0); if(flag) cout<<"NONE"<<endl; } return 0; }