fork download
  1. #include<bits/stdc++.h>
  2. // #pragma GCC optimize("unroll-loops" , "O3")
  3. // #pragma GCC target("avx2")
  4. using namespace std;
  5. #define int long long
  6. void run_case()
  7. {
  8. int n ;
  9. cin>>n;
  10. vector<int> occ(n + 1);
  11. vector<int> a(n);
  12. vector<int> f(n + 1);
  13. for(int i = 0 ; i < n ; i++)
  14. {
  15. cin>>a[i];
  16. occ[a[i]]++;
  17. }
  18. int ans = n;
  19. for(int i = 1 ; i <= n; i++)
  20. {
  21. for(int j = 2 * i ;j <= n ; j+=i)
  22. {
  23. f[i]+=occ[j];
  24. f[j]+=occ[i];
  25. }
  26. ans = min(ans , f[i] + (n - f[i] - occ[i]) * 2);
  27. }
  28. cout<<ans<<'\n';
  29. }
  30. signed main()
  31. {
  32. ios::sync_with_stdio(0);
  33. cin.tie(0);
  34. cout.tie(0);
  35. int t = 1;
  36. cin>>t;
  37. while (t--)
  38. {
  39. run_case();
  40. }
  41.  
  42. }
Runtime error #stdin #stdout #stderr 0.01s 5280KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc