fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. int n, k, x[100], a[100];
  7. bool ok = true;
  8.  
  9. void init(){
  10. cin >> n;
  11. for(int i = 1; i <= n; i++){
  12. x[i] = 0;
  13. }
  14. }
  15.  
  16. bool check(){
  17. int sum = 0;
  18. for(int i = 1; i <= n; i++){
  19. if(x[i] == 1){
  20. sum += a[i];
  21. }
  22. }
  23. return sum == k;
  24. }
  25.  
  26. void result(){
  27. for(int i = 1; i <= n; i++){
  28. if(x[i]) cout << a[i] << ' ';
  29. }
  30. cout << endl;
  31. }
  32.  
  33. void next_bits_string(){
  34. int i = n;
  35. while(i > 0 && x[i] == 1){
  36. x[i] = 0;
  37. i--;
  38. }
  39. if(i == 0){
  40. ok = false;
  41. }
  42. else{
  43. x[i] = 1;
  44. }
  45. }
  46.  
  47. int main(){
  48. init();
  49. cin >> k;
  50. for(int i = 1; i <= n; i++){
  51. cin >> a[i];
  52. }
  53. int cnt = 0;
  54. while(ok){
  55. if(check()){
  56. cnt++;
  57. result();
  58. }
  59. next_bits_string();
  60. }
  61. cout << cnt << endl;
  62. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
1