fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define nono \
  4.   ios::sync_with_stdio(0); \
  5.   cin.tie(0); \
  6.   cout.tie(0);
  7. using namespace std;
  8. set<int> primes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
  9. void solve(vector<int> ring, int n)
  10. {
  11. if (ring.size() == n)
  12. {
  13. if (primes.find(ring.back() + ring.at(0)) == primes.end())
  14. return;
  15. cout << 1 ;
  16. for (int i = 1; i < n; i++)
  17. cout << " " << ring.at(i);
  18. cout << '\n';
  19. return;
  20. }
  21. for (int circle = 2; circle < n + 1; ++circle)
  22. {
  23. bool flag = 1;
  24. for (auto a : ring)
  25. {
  26. if (a == circle)
  27. flag = 0;
  28. }
  29. if (primes.find(ring.back() + circle) == primes.end())
  30. flag = 0;
  31. if (flag)
  32. {
  33. ring.push_back(circle);
  34. solve(ring, n);
  35. ring.pop_back();
  36. }
  37. }
  38. }
  39. int main()
  40. {
  41. // nono;
  42. int i = 1, n;
  43. while (cin >> n && n != 0)
  44. {
  45. if(i>1) cout << "\n";
  46. cout<<"Case "<<i++<<":\n";
  47. vector <int > v;
  48. v.push_back(1);
  49. solve(v ,n);
  50.  
  51. }
  52.  
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0.01s 5304KB
stdin
Standard input is empty
stdout
Standard output is empty