amcbn

C2. No Cost Too Great (Hard Version)

Nov 8th, 2025
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int inf = 0x3f3f3f3f;
  4. #define all(x) begin(x), end(x)
  5.  
  6. template<typename T1, typename T2>
  7. bool ckmin(T1& a, T2 b) {
  8.     return a > b ? a = b, true : false;
  9. }
  10.  
  11. template<typename T1, typename T2>
  12. bool ckmax(T1& a, T2 b) {
  13.     return a < b ? a = b, true : false;
  14. }
  15.  
  16. const int nmax = 2e5;
  17. const int valmax = 2e5 + 5;
  18. int spf[valmax + 5];
  19. vector<int> factors[valmax + 5];
  20.  
  21. int n;
  22. int a[nmax + 5];
  23. int b[nmax + 5];
  24.  
  25. void testcase() {
  26.     cin >> n;
  27.     for (int i = 1; i <= n; ++i) {
  28.         cin >> a[i];
  29.     }
  30.     for (int i = 1; i <= n; ++i) {
  31.         cin >> b[i];
  32.     }
  33.     vector<int> ord(n);
  34.     iota(all(ord), 1);
  35.     sort(all(ord), [&](int i, int j) {
  36.         return b[i] < b[j];
  37.         });
  38.     // am incercat operatii pe 2 numere
  39.     long long answer = b[ord[0]] + b[ord[1]];
  40.     map<int, int> occ;
  41.     for (int i = 1; i <= n; ++i) {
  42.         for (auto& p : factors[a[i]]) {
  43.             if (occ[p] > 0) { // exista si altundeva factorul p
  44.                 answer = 0; // am incercat operatii pe 0 numere
  45.             }
  46.             occ[p]++;
  47.         }
  48.     }
  49.     // mai ramane sa incerc sa fac pe un singur numar
  50.     // incerc sa fac o singura operatie pe orice numar
  51.     for (int i = 1; i <= n; ++i) {
  52.         for (auto& p : factors[a[i]]) {
  53.             occ[p]--;
  54.         }
  55.         for (auto& p : factors[a[i] + 1]) {
  56.             if (occ[p] > 0) {
  57.                 ckmin(answer, b[i]);
  58.             }
  59.         }
  60.         for (auto& p : factors[a[i]]) {
  61.             occ[p]++;
  62.         }
  63.     }
  64.     // incerc sa fac operatii pe cel mai ieftin numar
  65.     for (auto& p : factors[a[ord[0]]]) {
  66.         occ[p]--;
  67.     }
  68.     for (auto& it : occ) {
  69.         if (it.second > 0) {
  70.             int p = it.first;
  71.             ckmin(answer, 1ll * (p - a[ord[0]] % p) % p * b[ord[0]]);
  72.         }
  73.     }
  74.     cout << answer << "\n";
  75. }
  76.  
  77. int main() {
  78.     ios::sync_with_stdio(0);
  79.     cin.tie(0);
  80.     for (int i = 1; i <= valmax; ++i) {
  81.         spf[i] = i;
  82.     }
  83.     for (int i = 2; i * i <= valmax; ++i) {
  84.         if (spf[i] == i) {
  85.             for (int j = i * i; j <= valmax; j += i) {
  86.                 ckmin(spf[j], i);
  87.             }
  88.         }
  89.     }
  90.     for (int i = 1; i <= valmax; ++i) {
  91.         int x = i;
  92.         while (x > 1) {
  93.             if (factors[i].empty() or factors[i].back() != spf[x]) {
  94.                 factors[i].push_back(spf[x]);
  95.             }
  96.             x /= spf[x];
  97.         }
  98.     }
  99.     int tc = 1;
  100.     cin >> tc;
  101.     for (int t = 1; t <= tc; ++t) {
  102. #ifdef _DEBUG
  103.         cerr << "=== Subtask " << t << " ===\n";
  104. #endif
  105.         testcase();
  106. #ifdef _DEBUG
  107.         cerr << "\n";
  108. #endif
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment