Advertisement
Ahmed_Negm

Untitled

Mar 6th, 2023
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6. #define ll long long
  7. #define OO 2'000'000'000
  8. #define ull unsigned long long
  9. #define nl '\n'
  10. #define sz(x) (ll)(x.size())
  11. #define all(x) x.begin(),x.end()
  12. #define rall(s)  s.rbegin(), s.rend()
  13. #define getline(s) getline(cin>>ws,s)
  14. #define ceill(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  15. #define pi  3.141592653589793
  16. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  17. #define multi_ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
  18.  
  19.  
  20. void Fast_IO(){
  21. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  22. // freopen("filename.in", "r", stdin);
  23. // freopen("filename.txt", "w", stdout);
  24. #ifndef ONLINE_JUDGE
  25. freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  26. #endif
  27. }
  28.  
  29.  
  30.  
  31.  
  32. int dx[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
  33. int dy[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
  34.  
  35. const ll MOD = 1e9+7;
  36.  
  37. int n;
  38. vector<ll> v;
  39.  
  40. bool check(vector<ll>& a) {
  41.     for(int i = 0; i < n-1; i++) {
  42.         if(a[i] % a[i+1] != 0 && a[i+1] % a[i] != 0) {
  43.             return false;
  44.         }
  45.     }
  46.     return true;
  47. }
  48.  
  49. ll count_permutations(int i) {
  50.     if(i == n) {
  51.         return check(v);
  52.     }
  53.     ll cnt = 0;
  54.     for(int j = i; j < n; j++) {
  55.         swap(v[i], v[j]);
  56.         cnt += count_permutations(i+1);
  57.         swap(v[i], v[j]);
  58.     }
  59.     return cnt;
  60. }
  61.  
  62.  
  63. void solve(){
  64.  
  65.  cin >> n;
  66.     v.resize(n);
  67.     for(auto& i : v) cin >> i;
  68.  
  69.     cout << count_permutations(0) << nl;
  70.  
  71.  
  72.  
  73.  
  74. }
  75.  
  76. int main(){
  77.     Fast_IO();
  78. int t =1;
  79. //cin>>t;
  80. while(t--){
  81. solve();
  82. }
  83. return 0;
  84. }  
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement