fork download
  1. /* ================================================
  2.  * _____ _____ _____ _____
  3.  * / ___| | _ \ |_ _| / ___|
  4.  * | | | |_| | | | | |___
  5.  * | | | _ / | | \___ \
  6.  * | |___ | | \ \ _| |_ ___| |
  7.  * \_____| |_| \_\|_____| |_____/
  8.  *
  9.  * "Quality is not an act, it is a habit."
  10.  ==================================================== */
  11. #include "bits/stdc++.h"
  12. #define int long long
  13. #define vi vector <int>
  14. #define vv vector <vector<int>>
  15. #define all(v) v.begin() , v.end()
  16. using namespace std;
  17.  
  18. template<typename T>
  19. istream &operator>>(istream &in, vector<T> &v) {
  20. for (auto &x: v) in >> x;
  21. return in;
  22. }
  23.  
  24. template<typename T>
  25. ostream &operator<<(ostream &out, const vector<T> &v) {
  26. for (size_t i = 0; i < v.size(); ++i) out << v[i] << (i + 1 == v.size() ? "" : " ");
  27. return out;
  28. }
  29.  
  30. const int N = 20, mod = 1e9 + 7;
  31. int n;
  32. bool used[17] = {false};
  33. vi v = {1};
  34. set <int> primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
  35.  
  36. void garb(int idx = 2, int cnt = 1){
  37. if(cnt == n){
  38. if(!primes.count(v[0] + v[n - 1])) return;
  39. cout << v << endl;
  40. return;
  41. }
  42. if(idx > n) return;
  43.  
  44. if(!used[idx] and primes.count(v.back() + idx)){
  45. used[idx] = true;
  46. v.push_back(idx);
  47. garb(2, cnt + 1);
  48.  
  49. v.pop_back();
  50. used[idx] = false;
  51. }
  52.  
  53. garb(idx + 1, cnt);
  54. }
  55.  
  56. void solve() {
  57. int c = 1;
  58. while(cin >> n){
  59. v.clear();
  60. v.push_back(1);
  61. cout << "Case " << c ++ << ":\n";
  62. garb();
  63. }
  64. }
  65.  
  66. signed main() {
  67. ios_base::sync_with_stdio(false);
  68. cin.tie(NULL);
  69.  
  70. int t = 1;
  71. // cin >> t;
  72. while (t--) {
  73. solve();
  74. }
  75.  
  76. return 0;
  77. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Standard output is empty