Advertisement
Saleh127

UVA 524

Aug 8th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int>xx;
  4. int n;
  5. int used[100];
  6. bool isprime(int a)
  7. {
  8. for (int i=2; i*i<=a; i++)
  9. {
  10. if (a%i==0) return false;
  11. }
  12. return true;
  13. }
  14. void backtrack(char x)
  15. {
  16. if(xx.size()==n)
  17. {
  18. if(isprime(xx[n-1]+1))
  19. {
  20.  
  21. printf("1");
  22. for(int i=1; i<xx.size();i++)
  23. {
  24. printf(" %d",xx[i]);
  25. }
  26. printf("\n");
  27. return;
  28.  
  29. }
  30. else return;
  31. }
  32. else
  33. {
  34. for(int i=2; i<=n; i++)
  35. {
  36. if(!used[i])
  37. {
  38. int l=xx.size()-1;
  39. if(isprime(i+xx[l]))
  40. {
  41. used[i]=1;
  42. xx.push_back(i);
  43. backtrack(i+1);
  44. xx.pop_back();
  45. used[i]=0;
  46. }
  47. }
  48. }
  49. }
  50. return;
  51.  
  52. }
  53. int main()
  54. {
  55. int cc=0;
  56. while(cin>>n)
  57. {
  58. cc++;
  59. if(cc!=1)printf("\n");
  60. printf("Case %d:\n",cc);
  61. xx.push_back(1);
  62. backtrack('x');
  63. xx.clear();
  64. }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement