fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define in cin
  4. #define out cout
  5. #define endl '\n'
  6. using namespace std;
  7.  
  8. bool isPrime(int n) {
  9. if (n <= 1) return false;
  10. for (int i = 2; i * i <= n; i++)
  11. if (n % i == 0) return false;
  12. return true;
  13. }
  14.  
  15. bool primes[100];
  16. vector<ll> ring;
  17. vector<bool> used(20);
  18.  
  19. void rec(ll n, ll index) {
  20. if (index == n) {
  21. if (primes[ring[n-1] + ring[0]]) {
  22. for (int i = 0; i < n; i++) out << ring[i] << ' ';
  23. out << endl;
  24. }
  25. return;
  26. }
  27.  
  28. for (int i = 2; i <= n; i++) {
  29. if (!used[i] && primes[ ring[index - 1] + i ]) {
  30. ring[index] = i;
  31. used[i] = true;
  32. rec(n, index + 1);
  33. used[i] = false;
  34. }
  35. }
  36. }
  37.  
  38. int main() {
  39. ios_base::sync_with_stdio(false);
  40. cin.tie(NULL);
  41.  
  42. for (int i = 1; i < 100; i++) primes[i] = isPrime(i);
  43.  
  44. ll n, cases = 1;
  45. while (in >> n) {
  46. if (cases > 1) out << endl;
  47. out << "Case " << cases++ << ":" << endl;
  48. ring.assign(n, 0);
  49. fill(used.begin(), used.end(), false);
  50. ring[0] = 1;
  51. used[1] = true;
  52. rec(n, 1);
  53. }
  54. }
  55.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty