Advertisement
Dang_Quan_10_Tin

DỰNG DÃY 2

Jun 5th, 2022
917
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. const int N = 1e5 + 5;
  6. vector<int> adj[N];
  7.  
  8. #define bit(i, x) (((x) >> (i)) & 1)
  9.  
  10. int p;
  11. struct T
  12. {
  13.     int a[N];
  14.     void Add(int x)
  15.     {
  16.         for (int i = 0; i < (1 << adj[x].size()); ++i)
  17.         {
  18.             int tmp(1);
  19.             for (int j = 0; j < adj[x].size(); ++j)
  20.                 if (bit(j, i))
  21.                     tmp *= adj[x][j];
  22.             ++a[tmp];
  23.         }
  24.     }
  25.     int Get(int x)
  26.     {
  27.         int ans(0);
  28.         for (int i = 0; i < (1 << adj[x].size()); ++i)
  29.         {
  30.             int tmp(1);
  31.             for (int j = 0; j < adj[x].size(); ++j)
  32.                 if (bit(j, i))
  33.                     tmp *= adj[x][j];
  34.             ans += a[tmp] * ((__builtin_popcount(i) & 1) ? -1 : 1);
  35.         }
  36.         return ans;
  37.     }
  38.     void Clear()
  39.     {
  40.         fill(a, a + p + 1, 0);
  41.     }
  42. } f, g;
  43. int n;
  44. int a[N], b[N], c[N], d[N], ida[N], idb[N];
  45.  
  46. void Read()
  47. {
  48.     cin >> n;
  49.     for (int i = 1; i <= n; ++i)
  50.         cin >> a[i];
  51.     for (int i = 1; i <= n; ++i)
  52.         cin >> b[i];
  53.     for (int i = 2; i <= n; ++i)
  54.         if (adj[i].empty())
  55.             for (int j = i; j <= n; j += i)
  56.                 adj[j].push_back(i);
  57. }
  58.  
  59. bool Check(int a[N], int b[N], int ida[N], int idb[N], int v)
  60. {
  61.     int j(1), t(1);
  62.     f.Clear();
  63.     g.Clear();
  64.     for (int i = 1; i <= p; ++i)
  65.     {
  66.         while (j <= p && a[ida[j]] + v <= b[idb[i]])
  67.         {
  68.             f.Add(ida[j]);
  69.             ++j;
  70.         }
  71.         while (t <= p && b[idb[t]] + v <= a[ida[i]])
  72.         {
  73.             g.Add(idb[t]);
  74.             ++t;
  75.         }
  76.         if (f.Get(idb[i]) > 0 || g.Get(ida[i]) > 0)
  77.             return true;
  78.     }
  79.     return false;
  80. }
  81.  
  82. void Solve()
  83. {
  84.     for (int i = 1; i <= n; ++i)
  85.     {
  86.         p = n / i;
  87.         for (int j = p; j; --j)
  88.         {
  89.             c[j] = a[j * i];
  90.             d[j] = b[j * i];
  91.             ida[j] = idb[j] = j;
  92.         }
  93.         sort(ida + 1, ida + p + 1, [&](const int &x, const int &y) {
  94.             return c[x] < c[y];
  95.         });
  96.         sort(idb + 1, idb + p + 1, [&](const int &x, const int &y) {
  97.             return d[x] < d[y];
  98.         });
  99.         int l = 0, m, h = 1e9 - 1;
  100.         while (l <= h)
  101.         {
  102.             m = (l + h) / 2;
  103.             if (Check(c, d, ida, idb, m))
  104.                 l = m + 1;
  105.             else
  106.                 h = m - 1;
  107.         }
  108.         cout << h << " ";
  109.     }
  110. }
  111.  
  112. int32_t main()
  113. {
  114.     ios::sync_with_stdio(0);
  115.     cin.tie(0);
  116.     cout.tie(0);
  117.     Read();
  118.     Solve();
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement