Advertisement
maycod23

Untitled

Jun 20th, 2022
1,086
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. vector<int> getPrimeFactors(int a, vector<int> & primes)
  5. {
  6.     vector<int> f;
  7.     for (auto p : primes)
  8.     {
  9.         if (p > a) break;
  10.         if (a % p == 0)
  11.         {
  12.             f.push_back(p);
  13.             do
  14.             {
  15.                 a /= p;
  16.             } while (a % p == 0);
  17.         }
  18.     }
  19.     if (a > 1) f.push_back(a);
  20.     return f;
  21. }
  22. void solution(vector<int> & A, vector<int> & B)
  23. {
  24.     vector<int> primes;
  25.     primes.push_back(2);
  26.  
  27.     for (int i = 3; i * i <= 1e9; ++i)
  28.     {
  29.         bool isPrime = true;
  30.         for (auto p : primes)
  31.         {
  32.             if (i % p == 0)
  33.             {
  34.                 isPrime = false;
  35.                 break;
  36.             }
  37.         }
  38.         if (isPrime)
  39.         {
  40.             primes.push_back(i);
  41.         }
  42.     }
  43.     int N = A.size();
  44.     map<int, int> cntp;
  45.     for (int i = 0; i < N; i++)
  46.     {
  47.         auto f = getPrimeFactors(A[i], primes);
  48.         vector<pair<int, int>> x;
  49.         x.push_back({ 0, 1 });
  50.         for (auto p : f)
  51.         {
  52.             int k = x.size();
  53.             for (int i = 0; i < k; ++i)
  54.             {
  55.                 int nn = x[i].first + 1;
  56.                 int pp = x[i].second * p;
  57.  
  58.                 ++cntp[pp];
  59.                 x.push_back({ nn, pp });
  60.             }
  61.         }
  62.     }
  63.     int cnt = N; cnt *= N;
  64.     for (int i = 0; i < N; i++)
  65.     {
  66.         auto f = getPrimeFactors(B[i], primes);
  67.         vector<pair<int, int>> x;
  68.         x.push_back({ 0, 1 });
  69.         for (auto p : f)
  70.         {
  71.             int k = x.size();
  72.             for (int i = 0; i < k; ++i)
  73.             {
  74.                 int nn = x[i].first + 1;
  75.                 int64_t pp = x[i].second * p;
  76.                 x.push_back({ nn, pp });
  77.                 if (nn % 2 == 1)
  78.                 {
  79.                     cnt -= cntp[pp];
  80.                 }
  81.                 else
  82.                 {
  83.                     cnt += cntp[pp];
  84.                 }
  85.             }
  86.         }
  87.     }
  88.     int temp = A.size() * A.size();
  89.     cout << temp - cnt << endl;
  90. }
  91. int main()
  92. {
  93.     int n; cin >> n;
  94.     vector<int> a(n), b(n);
  95.     for (int i = 0; i < n; i++) cin >> a[i];
  96.     for (int i = 0; i < n; i++) cin >> b[i];
  97.     solution(a, b);
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement