fork download
  1. // #include<bits/stdc++.h>
  2. // using namespace std ;
  3. // #define endl "\n"
  4. // int n , k , a[100] , sum = 0 , x[100];
  5. // bool used[105];
  6. // int check = 0 ;
  7. // // cnt la de kiem tra xem da tao duoc bao nhieu mang
  8. // void Try(int i , int start , int currentSum , int cnt){
  9. // // neu bat dau quay lui sang cnt == k + 1 thi da du k mang theo yeu cau
  10. // if(cnt == k + 1){
  11. // check = 1 ;
  12. // return ;
  13. // }
  14. // for(int j = start ; j <= n ; j++){
  15. // if(!used[j] && currentSum + a[j] <= sum){
  16. // x[i] = a[j];
  17. // used[j] = true ;
  18. // //da chia duoc thanh 1 mang co tong = sum => gan lai start = 1 , currentSum = 0
  19. // if(currentSum + a[j] == sum){
  20. // Try(i + 1 , 1 , 0, cnt + 1);
  21. // }
  22. // else{
  23. // Try(i + 1 , j + 1 , currentSum + a[j] , cnt);
  24. // }
  25. // // backtrack
  26. // used[j] = false ;
  27. // }
  28. // }
  29. // }
  30. // int main(){
  31. // ios::sync_with_stdio(false);
  32. // cin.tie(nullptr);
  33. // cout.tie(nullptr);
  34.  
  35. // cin >> n >> k ;
  36. // for(int i = 1 ; i <= n ; i++){
  37. // cin >> a[i];
  38. // sum += a[i];
  39. // }
  40.  
  41. // if(sum % k == 0){
  42. // sum /= k ; // tinh luon
  43. // Try(1 , 1 , 0 , 1); // cnt = 1 => thi dieu kien tren la cnt == k + 1 || cnt = 0 => dk tren la cnt == k
  44. // cout << check ;
  45. // }
  46. // else cout << "0";
  47. // }
  48.  
  49. #include <bits/stdc++.h>
  50. #define endl "\n"
  51. using namespace std;
  52. int n, k, sum;
  53. bool ans = 0;
  54. int a[100];
  55. void Try(int s, int tmp){
  56. if (ans){
  57. return;
  58. }
  59. if (tmp == k){
  60. ans = 1;
  61. return;
  62. }
  63. for (int i = 0; i < n; i++){
  64. if (s == sum){
  65. Try(0, tmp + 1);
  66. cout << "Try(0,tmp + 1) : " << tmp + 1 << endl ;
  67. }
  68. else if (s < sum){
  69. Try(s + a[i], tmp);
  70. cout << "Try( " << s + a[i]<< "," << tmp << " )" << endl ;
  71. }
  72. else return ;
  73. }
  74. }
  75.  
  76. int main(){
  77. ios::sync_with_stdio(false);
  78. cin.tie(nullptr);
  79. cout.tie(nullptr);
  80.  
  81. cin >> n >> k;
  82. sum = 0;
  83. ans = 0;
  84. for (int i = 0; i < n; i++){
  85. cin >> a[i];
  86. sum += a[i];
  87. }
  88.  
  89. if (sum % k != 0)
  90. cout << "0";
  91. else{
  92. sum /= k;
  93. Try(0, 0);
  94. // cout << ans;
  95. }
  96. }
Success #stdin #stdout 0.01s 5308KB
stdin
5 3 
2 1 4 5 6
stdout
Try(0,tmp + 1) : 3
Try(0,tmp + 1) : 3
Try(0,tmp + 1) : 3
Try(0,tmp + 1) : 3
Try(0,tmp + 1) : 3
Try( 6,2 )
Try( 5,2 )
Try( 8,2 )
Try( 9,2 )
Try( 10,2 )
Try( 4,2 )
Try( 3,2 )
Try( 6,2 )
Try( 7,2 )
Try( 8,2 )
Try( 2,2 )
Try( 1,2 )
Try( 4,2 )
Try( 5,2 )
Try( 6,2 )
Try(0,tmp + 1) : 2
Try(0,tmp + 1) : 2
Try(0,tmp + 1) : 2
Try(0,tmp + 1) : 2
Try(0,tmp + 1) : 2
Try( 6,1 )
Try( 5,1 )
Try( 8,1 )
Try( 9,1 )
Try( 10,1 )
Try( 4,1 )
Try( 3,1 )
Try( 6,1 )
Try( 7,1 )
Try( 8,1 )
Try( 2,1 )
Try( 1,1 )
Try( 4,1 )
Try( 5,1 )
Try( 6,1 )
Try(0,tmp + 1) : 1
Try(0,tmp + 1) : 1
Try(0,tmp + 1) : 1
Try(0,tmp + 1) : 1
Try(0,tmp + 1) : 1
Try( 6,0 )
Try( 5,0 )
Try( 8,0 )
Try( 9,0 )
Try( 10,0 )
Try( 4,0 )
Try( 3,0 )
Try( 6,0 )
Try( 7,0 )
Try( 8,0 )
Try( 2,0 )
Try( 1,0 )
Try( 4,0 )
Try( 5,0 )
Try( 6,0 )