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 % (j+1) != 0) continue;
  24. b[j] = true;
  25. r.push_back(j+1);
  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;
  39. cin >> N;
  40. for(int i = 0; i < N; ++i) v.push_back(false);
  41.  
  42. vector<int> r;
  43. test(v,0,r);
  44.  
  45. }
  46.  
Success #stdin #stdout 1.39s 4276KB
stdin
35
stdout
  3  1  4  8 16  2 34 17  5 15  7 28 35 25 20 11 33 24  9 27 18  6 29 13 26 32 14 21 23 22 12 30 19 31 10