Loading

[ARC122E] Increasing LCMs 题解

感觉像比较套路的构造题。

思路

假如我们正着进行构造,可以发现我们加入一个数以后,对后面的数产生的影响是很大的。

但是如果我们从最后一个数开始构造,那么可以发现它是不会对之后的构造产生任何影响的。

应为越前面的数的限制会越少,那么可以填的数一定是不减的。

一个数可以填在后面,那么也一定可以填在前面。

所以我们现在只需要不断找到可以填的数即可。

考虑限制是什么。

要求:

\[a_i\not |\operatorname{lcm}_{i=1}^{j-1}a_j \]

但是 \(\operatorname{lcm}\) 太大了,我们需要简化一下,相当于:

\[\gcd(a_i,\operatorname{lcm}_{i=1}^{j-1}a_j)<a_i \]

\[\operatorname{lcm}_{i=1}^{j-1}\gcd(a_i,a_j)<a_i \]

这样就可以处理了。

时间复杂度:\(O(n^3\log n)\)

Code

#include <bits/stdc++.h>
using namespace std;

#define int long long

int n, a[110], b[110], v[110];

inline int lcm(int x, int y) {
  int g = __gcd(x, y);
  return (__int128) x * y / g;
}

signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= n; j++) {
      if (v[j]) continue;
      int x = 0;
      for (int k = 1; k <= n; k++) {
        if (v[k] || k == j) continue;
        if (x == 0) x = __gcd(a[j], a[k]);
        x = lcm(__gcd(a[j], a[k]), x);
      }
      if (x < a[j]) {
        v[j] = 1, b[i] = a[j];
        break;
      }
    }
    if (b[i] == 0) cout << "No\n", exit(0);
  }
  cout << "Yes\n";
  for (int i = 1; i <= n; i++) cout << b[i] << " \n"[i == n];
}
posted @ 2024-09-25 11:48  JiaY19  阅读(6)  评论(0)    收藏  举报