fork download
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <numeric>
  6.  
  7. using namespace std;
  8.  
  9. vector<bool> v;
  10.  
  11. bool test(vector<bool>&b, int n, vector<int>&r)
  12. {
  13. if (n == b.size())
  14. {
  15. for(auto i: r) cout << setw(3) << i; cout << endl;
  16. return true;
  17. }
  18. int sum = accumulate(r.begin(),r.end(),0);
  19. for(int j = 0; j < b.size(); ++j)
  20. {
  21. if (b[j] == false)
  22. {
  23. if (sum % (b.size()-j) != 0) continue;
  24. b[j] = true;
  25. r.push_back(b.size()-j);
  26. bool res = test(b,n+1,r);
  27. r.pop_back();
  28. b[j] = false;
  29. if (res) return true;
  30. }
  31. }
  32. return false;
  33. }
  34.  
  35.  
  36. int main(int argc, const char * argv[])
  37. {
  38. int N; cin >> N;
  39. for(int i = 0; i < N; ++i) v.push_back(false);
  40.  
  41. vector<int> r;
  42. test(v,0,r);
  43.  
  44. }
  45.  
  46.  
Success #stdin #stdout 3.72s 4200KB
stdin
60
stdout
 60 30 45 27 54 36 42 49  7 50 40 55 33 48 32 38 34 20 35 21 28 56 24 18 14 16 57 51 15  5 26 41  9 31 37  8  4 46  6 52 25 53  2 12 58 29  3 13 23 22 11 47 17 19 43 39 44 10 59  1