Advertisement
pb_jiang

t3

May 25th, 2024
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. using ll = long long;
  2. vector<int> primes, not_prime, prime_factor;
  3.  
  4. class Solution {
  5.     static void get_prime(int n) {
  6.         if (primes.size())
  7.             return;
  8.         not_prime = vector<int>(n + 1);
  9.         prime_factor = vector<int>(n + 1);
  10.         for (int i = 2; i <= n; ++i) {
  11.             if (!not_prime[i]) {
  12.                 primes.push_back(i);
  13.                 prime_factor[i] = i;
  14.             }
  15.             for (int j = 0; j < primes.size() && primes[j] * i <= n; ++j) {
  16.                 not_prime[i * primes[j]] = 1;
  17.                 prime_factor[i * primes[j]] = primes[j];
  18.                 if (i % primes[j] == 0)
  19.                     break;
  20.             }
  21.         }
  22.     }
  23. public:
  24.     long long numberOfPairs(vector<int>& ns1, vector<int>& ns2, int k) {
  25.         ll ret = 0;
  26.         vector<int> ns3;
  27.         for (auto x: ns1)
  28.             if (x % k == 0)
  29.                 ns3.push_back(x / k);
  30.         map<int, int> ns2cnt;
  31.         for (auto x: ns2) ns2cnt[x] += 1;
  32.         sort(ns3.begin(), ns3.end());
  33.         map<int, set<int>> ns3set;
  34.         for (auto x: ns3) {
  35.             if (ns3set.count(x))
  36.                 continue;
  37.             for (int i = 1; i * i <= x; ++i)
  38.                 if (x % i == 0)
  39.                     ns3set[x].insert(x / i), ns3set[x].insert(i);
  40.         }
  41.         for (auto x: ns3)
  42.             for (auto f: ns3set[x])
  43.                 ret += ns2cnt[f];
  44.         return ret;
  45.     }
  46.     long long numberOfPairs1(vector<int>& ns1, vector<int>& ns2, int k) {
  47.         constexpr int ub = 1e6 + 3;
  48.         get_prime(ub);
  49.  
  50.         ll ret = 0;
  51.         vector<int> ns3;
  52.         for (auto x: ns1)
  53.             if (x % k == 0)
  54.                 ns3.push_back(x / k);
  55.         vector<int> ns2cnt(ub);
  56.         for (auto x: ns2) ns2cnt[x] += 1;
  57.         for (auto p: primes)
  58.             for (int i = 1; i * p < ub; ++i)
  59.                 ns2cnt[i * p] += ns2cnt[i];
  60.  
  61.         for (auto x: ns3)
  62.             ret += ns2cnt[x];
  63.         return ret;
  64.     }
  65. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement