fork download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5.  
  6. #define mem(a,b) memset(a,b,sizeof(a))
  7. using namespace std;
  8.  
  9. int a[25];
  10. int vis[25];
  11.  
  12.  
  13. bool isPrim(int n)
  14. {
  15. if(n<4)
  16. return true;
  17. if(n%2==0)
  18. return false;
  19.  
  20. for(int i=3; i<=sqrt(n); i++)
  21. {
  22. if(n%i==0)
  23. return false;
  24. }
  25. return true;
  26. }
  27.  
  28. void DFS(int k,int m)
  29. {
  30. if(k==m+1)
  31. {
  32. if(isPrim(a[k-1]+a[1]))
  33. {
  34. for(int i=1; i<=m; i++)
  35. {
  36. printf("%d",a[i]);
  37. if(i==m)
  38. printf("\n");
  39. else
  40. printf(" ");
  41. }
  42. }
  43. return;
  44. }
  45. for(int i=1; i<=m; i++)
  46. {
  47. if(isPrim(a[k-1]+i)&&vis[i]==0)
  48. {
  49. a[k]=i;
  50. vis[i]=1;
  51. DFS(k+1,m);
  52. vis[i]=0;
  53. }
  54. }
  55. }
  56.  
  57. int main()
  58. {
  59. int n,ca=1;
  60. while(scanf("%d",&n)!=EOF)
  61. {
  62. printf("Case %d:\n",ca++);
  63. mem(vis,0);
  64. a[1]=1;
  65. vis[1]=1;
  66. DFS(2,n);
  67. printf("\n");
  68. }
  69. return 0;
  70. }
  71.  
Success #stdin #stdout 0s 4548KB
stdin
4
stdout
Case 1:
1 2 3 4
1 4 3 2