fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int arr[20], nofs = 0;
  8.  
  9. void gen(const vector<int> &mvc, vector<string> &res, int n, int i = 0)
  10. {
  11. if (i == n) {
  12. int a, b;
  13. string s;
  14. int sumofa = 0, sumofb = 0;
  15. for(int j = 0; j < n; ++j) {
  16. if (arr[j] == 0) {
  17. s.push_back('A');
  18. sumofa += mvc[i];
  19. }
  20. else {
  21. s.push_back('B');
  22. sumofb += mvc[i];
  23. }
  24. }
  25. if (sumofa == sumofb) {
  26. res.push_back(s);
  27. ++nofs;
  28. }
  29. }
  30. else {
  31. arr[i] = 0;
  32. gen(mvc, res, n, i + 1);
  33. arr[i] = 1;
  34. gen(mvc, res, n, i + 1);
  35. }
  36. }
  37.  
  38. int main()
  39. {
  40. int n;
  41. vector<int> mvc; // store input sequence
  42. vector<string> res; // store result
  43. cin >> n;
  44. mvc.resize(n);
  45. res.resize(n);
  46. for (int i = 0; i < n; ++i) {
  47. res[i].resize(n);
  48. cin >> mvc[i];
  49. }
  50. gen(mvc, res, n);
  51. if (nofs > 0) {
  52. cout << nofs << endl;
  53. for (int i = 0; i < nofs; ++i) {
  54. for (int j = 0; j < n; ++j) {
  55. cout << res[i][j] << " ";
  56. }
  57. cout << endl;
  58. }
  59. }
  60. else cout << "khong chia duoc";
  61. return 0;
  62. }
Success #stdin #stdout 0s 15240KB
stdin
6 1 2 2 5 10 10
stdout
20
      
      
      
      
      
      
A A A B B B 
A A B A B B 
A A B B A B 
A A B B B A 
A B A A B B 
A B A B A B 
A B A B B A 
A B B A A B 
A B B A B A 
A B B B A A 
B A A A B B 
B A A B A B 
B A A B B A 
B A B A A B