fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6.  
  7. #define el '\n'
  8. #define MOD 1000000007
  9.  
  10. int n;
  11.  
  12. const int SIZE = 100;
  13. const int LIMIT = sqrt(SIZE)+1;
  14.  
  15. bitset<SIZE> prime;
  16.  
  17. void sieve() {
  18. prime.flip();
  19. prime[1]=0;
  20. for(int i=2;i<=LIMIT;i++){
  21. if (prime[i]){
  22. for(int j=i*i;j<SIZE;j+=i){
  23. prime[j]=0;
  24. }
  25. }
  26. }
  27. }
  28.  
  29. bool isPrime(int kov) {
  30. return prime[kov];
  31. }
  32.  
  33. void gensol(int yey, vector<int> ge, vector<int> ga, vector<int> hm){
  34. if(hm.size()==n){
  35. int lastidx = hm.size()-1;
  36. if(isPrime(1+hm[lastidx])){
  37. for(auto x:hm){
  38. cout << x << " ";
  39. }
  40. cout << el;
  41. }
  42. } else {
  43. if(yey%2==1){
  44. vector<int> newge;
  45. for(int i=0;i<ge.size();i++){
  46. newge.push_back(ge[i]);
  47. }
  48. for(int i=0;i<ge.size();i++){
  49. if(isPrime(yey+ge[i])){
  50. hm.push_back(ge[i]);
  51. vector<int> newnewge;
  52. for(int q=0;q<newge.size();q++){
  53. if(newge[q]!=ge[i]){
  54. newnewge.push_back(newge[q]);
  55. }
  56. }
  57. gensol(ge[i], newnewge, ga, hm);
  58. hm.pop_back();
  59. }
  60. }
  61. } else {
  62. vector<int> newga;
  63. for(int i=0;i<ga.size();i++){
  64. newga.push_back(ga[i]);
  65. }
  66. for(int i=0;i<ga.size();i++){
  67. if(isPrime(yey+ga[i])){
  68. hm.push_back(ga[i]);
  69. vector<int> newnewga;
  70. for(int q=0;q<newga.size();q++){
  71. if(newga[q]!=ga[i]){
  72. newnewga.push_back(newga[q]);
  73. }
  74. }
  75. gensol(ga[i], ge, newnewga, hm);
  76. hm.pop_back();
  77. }
  78. }
  79. }
  80. }
  81. }
  82. int main() {
  83. ios_base::sync_with_stdio(false);
  84. cin.tie(NULL);
  85. cout.tie(NULL);
  86. sieve();
  87. int t=1;
  88. while(cin >> n){
  89. vector<int> genap;
  90. vector<int> ganjil;
  91. for(int i=2;i<=n;i+=2){
  92. genap.push_back(i);
  93. }
  94. for(int i=3;i<=n;i+=2){
  95. ganjil.push_back(i);
  96. }
  97.  
  98. cout << "Case #" << t << ":" << el;
  99. vector<int> res;
  100. res.push_back(1);
  101. gensol(1, genap, ganjil, res);
  102. t++;
  103. cout << el;
  104. }
  105. return 0;
  106. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty