Jeremiah_

Problem D - round #565 CF

Feb 5th, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define SYNC ios::sync_with_stdio(0);
  4. #define F first
  5. #define S second
  6. #define endl '\n'
  7.  
  8.  
  9. using namespace std;
  10.  
  11. using ll = long long int;
  12. using ii = pair<int, int>;
  13. using vii = vector<ii>;
  14. using vi = vector<int>;
  15. using graph = vector<vi>;
  16. const int INF = 0x3f3f3f3f;
  17. const int MAXN = 199999;
  18. const ll mod = 1000000007;
  19. const double eps = 0.000000001;
  20.  
  21.  
  22. int largest_divisor(int n) {
  23.   if (n % 2 == 0) {
  24.     return n/2;
  25.   }
  26.  
  27.   int sq = sqrt(n);
  28.   for (int i = 3; i <= sq; i += 2) {
  29.     if (n % i == 0) {
  30.       return n/i;
  31.     }
  32.   }
  33.   return 1;
  34.  
  35. }
  36.  
  37. bool is_prime(int n) {
  38.   if (n <= 3) {
  39.     return n > 1;
  40.   }
  41.   if (n % 2 == 0 || n % 3 == 0) {
  42.     return false;
  43.   }
  44.   for (int i = 5; i * i <= n ;i += 6) {
  45.     if (n % i == 0 || n % (i+2) == 0) {
  46.       return false;
  47.     }
  48.  
  49.   }
  50.   return true;
  51. }
  52.  
  53. int primes_to_idx[2750132];
  54. int freq[2750132];
  55.  
  56. int main() {
  57.   ios::sync_with_stdio(0);
  58.   for (int i = 0, j = 1; j <= MAXN; i++) {
  59.     if (is_prime(i)) {
  60.       primes_to_idx[i] = j;
  61.       j++;
  62.     }
  63.   }
  64.   int n;
  65.   cin >> n;
  66.   for (int i = 0, num; i < 2*n; i++) {
  67.     cin >> num;
  68.     freq[num]++;
  69.   }
  70.   vector<int> ans;
  71.  
  72.   for (int i = 2750131; i >= 0; --i) {
  73.     if (freq[i]) {
  74.       int ld = largest_divisor(i);
  75.       if (ld == 1) {
  76.         freq[primes_to_idx[i]] -= freq[i];
  77.         for (int j = 0; j < freq[i]; j++) ans.push_back(primes_to_idx[i]);
  78.         freq[i] = 0;
  79.       } else {
  80.         for (int j = 0; j < freq[i]; j++) ans.push_back(i);
  81.         freq[ld] -= freq[i];
  82.         freq[i] = 0;
  83.       }
  84.     }
  85.   }
  86.   for (auto a: ans) cout << a << ' ';
  87.   cout << endl;
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment