Skip to content

Instantly share code, notes, and snippets.

@behitek
Created October 31, 2016 02:14
Show Gist options
  • Save behitek/68dfd4c8c3bd6d856a3b0a342180efd6 to your computer and use it in GitHub Desktop.
Save behitek/68dfd4c8c3bd6d856a3b0a342180efd6 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
#define _for(i,a,b) for (int i=(a),_b_=(b);i<_b_;i++)
#define _fod(i,a,b) for (int i=(a),_b_=(b);i>_b_;i--)
#define _it(i,v) for (typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define _all(v) v.begin(), v.end()
#define __(v) memset(v,0,sizeof(v))
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
template<typename T> vector<T> &operator += (vector<T> &v, T x) {v.push_back(x);return v;}
void solve() {
int n,m;
cin>>m>>n;
int arr[n];
_for(i,0,n) cin>>arr[i];
int d[n];
int k = 1,sum;
while(k <= n){
int a[k];
_for(i,0,k){
d[i] = i;
}
bool True = 1;
while(True){
sum = 0;
int u = 0;
_for(i,0,k){
sum += arr[d[i]];
a[u++] = d[i];
}
if(sum == m){
_for(l,0,u) cout<<arr[a[l]]<<" ";
cout<<endl;
}
_fod(i,k-1,-1){
if(d[i] < n - k +i){
d[i]+= 1;
_for(j,i+1,n) d[j] = d[j-1]+1;
break;
}else if(i == 0 && d[i] == n-k){
True = 0;
break;
}
}
}
k++;
}
// if(nMin > 1e9) cout<<(-1)<<endl;
// else cout<<nMin<<endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
}
cho trước một số S một dãy số hạng đã được sắp xếp trước.
tìm tất cả các phần tử trong dãy có tổng bằng S cho trước
ví dụ: cho số 100 và dãy 70, 60, 50,40, 30, 20, 10
kết quả cho ra là 6 dãy số.
70, 30.
70, 20, 10.
60, 40.
60, 30, 10.
50, 40, 10.
40, 30, 20, 10.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment