Advertisement
Ahmed_Negm

A

Oct 23rd, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 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 ull unsigned long long
  8. #define nl '\n'
  9. #define sz(x) (ll)(x.size())
  10. #define all(x) x.begin(),x.end()
  11. #define rall(s)  s.rbegin(), s.rend()
  12. #define getline(s) getline(cin>>ws,s)
  13. #define ceill(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  14. #define pi  3.141592653589793
  15. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  16. #define multi_ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
  17.  
  18.  
  19. void Fast_IO(){
  20. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  21. // freopen("filename.in", "r", stdin);
  22. // freopen("filename.txt", "w", stdout);
  23. #ifndef ONLINE_JUDGE
  24. freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  25. #endif
  26. }
  27.  
  28. int dx[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
  29. int dy[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
  30.  
  31. bool good(vector<ll>&v){
  32.     ll GCD = 0;
  33.     for(int i = 0;i < sz(v);i++){
  34.         GCD = __gcd(GCD,v[i]);
  35.     }
  36.     return GCD == 1;
  37. }
  38.  
  39.  
  40.  
  41. void solve(){
  42.    
  43.     ll n; cin>>n;
  44.     vector<ll> v(n);
  45.     ll cost = LONG_LONG_MAX;
  46.     bool one = 0;
  47.     for(int i=0;i<n;i++) cin>>v[i], one |= v[i] == 1;
  48.     if(one) return cout<<0<<nl, void();
  49.  
  50.     for(int i=0; i<(1<<n); i++){
  51.         vector<ll> tmp = v;
  52.         ll cur_cost = 0;
  53.         for(int j=0; j<n; j++){
  54.             if(i&(1<<j)){
  55.                 tmp[j] = gcd(tmp[j],j+1);
  56.                 cur_cost += n-j;
  57.             }
  58.         }
  59.         if(good(tmp)){
  60.             cost = min(cost,cur_cost);
  61.         }
  62.     }
  63. cout<<cost<<nl;
  64.  
  65.  
  66. }
  67.  
  68. int main(){
  69.     Fast_IO();
  70. int t =1;
  71. cin>>t;
  72. while(t--){
  73. solve();
  74. }
  75. return 0;
  76. }  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement