Advertisement
Anwar_Rizk

Q. Prims and Permutation

Sep 25th, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define cin(vec) for(auto& i : vec) cin >> i
  4. #define cin_2d(vec, n, m) for(int i=0; i<n; i++) for(int j=0; j<m && cin >> vec[i][j]; j++);
  5. #define cout(vec) for(auto& i : vec) cout << i << " "; cout << "\n";
  6. #define cout_2d(vec, r, c) for(int i=0; i<r; i++, cout << "\n") for(int j=0; j<c && cout << vec[i][j] << " "; j++);
  7. #define cout_map(mp) for(auto& [f, s] : mp) cout << f << " : " << s << "\n";
  8. #define matrix(grid, n, m) vector < vector <ll> > grid(n, vector <ll> (m));
  9. #define Time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs" << "\n";
  10. #define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  11. #define fixed(n) cout << fixed << setprecision(n)
  12. #define Num_of_Digits(n) ((int)log10(n)+1)
  13. #define getline(s) getline(cin >> ws, s)
  14. #define to_decimal(bin) stoi(bin, nullptr, 2)
  15. #define TC int t; cin >> t; while(t--)
  16. #define rall(s) s.rbegin(), s.rend()
  17. #define all(s) s.begin(), s.end()
  18. #define sz(x) int(x.size())
  19. #define Pair pair <int, int>
  20. #define fi first
  21. #define se second
  22. #define ll long long
  23. #define PI acos(-1)
  24. #define Mod 1'000'000'007
  25. #define INF 2'000'000'000
  26. #define EPS 1e-9
  27.  
  28. void Anwar_Rizk(){
  29.   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  30.   #ifndef ONLINE_JUDGE    // Anwar Rizk 🥇🖤
  31.     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  32.   #endif
  33.   Time
  34. }
  35.  
  36. bool is_prime(ll n){
  37.   if(n < 2 || (n % 2 == 0 && n != 2)) return false;
  38.   for(int i = 3; i <= sqrt(n); i += 2)
  39.       if(n % i == 0) return false;
  40.   return true;
  41. }
  42.  
  43. bool is_good(vector <int>& v){
  44.   for(int i = 1; i < sz(v); i++){
  45.     if(!is_prime(v[i] + v[i - 1]))  return false;
  46.   }
  47.   return true;
  48. }
  49.  
  50. int n;
  51. set < vector < int > > answer;
  52. vector < int > nums;
  53.  
  54. void permut(int l, int r){
  55.   if(l == r){
  56.     if(nums.front() == 1 && is_good(nums))
  57.     answer.emplace(nums);
  58.     return;
  59.   }
  60.   for(int i = l; i <= r; i++){
  61.     swap(nums[l], nums[i]);
  62.     permut(l + 1, r);
  63.     swap(nums[l], nums[i]);
  64.   }
  65. }
  66.  
  67. int main()
  68. {   Anwar_Rizk();
  69.  
  70.     cin >> n;
  71.     nums.resize(n);
  72.     for(int i = 0; i < n; i++)  nums[i] = i + 1;
  73.     permut(0, n - 1);
  74.     for(auto& v : answer){
  75.       cout(v);
  76.     }
  77.    
  78.   return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement