fork download
  1. #include <bits/stdc++.h>
  2.  
  3. #define name "test"
  4. #define FOR(i, a, b) for (int i = a; i <= b; ++i)
  5. #define FORD(i, a, b) for (int i = a; i >= b; --i)
  6. #define ll long long
  7. #define fi first
  8. #define se second
  9. #define ii pair <int, int>
  10. #define sz(a) a.size()
  11. #define pb push_back
  12. #define MASK(x) (1ll << x)
  13. #define bit(x, i) ((x >> i) & 1)
  14. #define pop_count(x) __builtin_popcount(x)
  15. #define mingdu signed main()
  16.  
  17. using namespace std;
  18.  
  19. const ll mod = 1e9 + 7;
  20.  
  21. void add(ll& a, ll b) {
  22. a += b;
  23. if (a >= mod) a -= mod;
  24. if (a < 0) a += mod;
  25. }
  26.  
  27. template<class X, class Y> void mx(X &x, const Y y) {if (y > x) x = y;}
  28. template<class X, class Y> void mn(X &x, const Y y) {if (y < x) x = y;}
  29.  
  30. const int N = 1e6 + 5;
  31. int a[N], b[N], n, m;
  32.  
  33. void nhap() {
  34. cin >> n >> m;
  35. FOR(i, 1, n) cin >> a[i];
  36. FOR(i, 1, m) cin >> b[i];
  37. }
  38.  
  39. void giai() {
  40. map <int, int> mpA, mpB;
  41. map <pair<int, int>, int> cnt;
  42. int Max = 0;
  43. FOR(i, 1, n) ++mpA[a[i]];
  44. FOR(i, 1, m) ++mpB[b[i]];
  45.  
  46. for (auto [x1, v1] : mpA) for (auto [x2, v2] : mpB) {
  47. int g = __gcd(x1, x2);
  48. int tu = x1 / g;
  49. int mau = x2 / g;
  50. Max = max(Max, cnt[{tu, mau}] += min(v1, v2));
  51. }
  52.  
  53. cout << n + m - Max << '\n';
  54. }
  55.  
  56. mingdu {
  57. ios_base::sync_with_stdio(0);
  58. cin.tie(0); cout.tie(0);
  59.  
  60. if (fopen(name".inp", "r")) {
  61. freopen(name".inp", "r", stdin);
  62. freopen(name".out", "w", stdout);
  63. }
  64.  
  65. int t; cin >> t;
  66. while (t--) {
  67. nhap();
  68. giai();
  69. }
  70.  
  71. return 0;
  72. }
  73.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty