题解 ARC122E【Increasing LCMs】
注意到如果一个序列合法,那么其子序列必然合法。
若一个元素
此时,若其他元素可以构成合法的序列,那么将
因此将
但是上式的值太大了,无法直接计算。考虑转化为
时间复杂度为
代码如下:
#include <bits/stdc++.h>
using namespace std;
int n;
long long a[110];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
bool flg = true;
for (int k = n; k >= 1; k--) {
bool ok = false;
for (int i = 1; i <= k; i++) {
long long num = 1LL;
for (int j = 1; j <= k; j++) {
if (j != i) {
long long tmp = __gcd(a[i], a[j]);
num *= tmp / __gcd(tmp, num);
}
}
if (num != a[i]) {
swap(a[i], a[k]);
ok = true;
break;
}
}
if (!ok) {
flg = false;
break;
}
}
if (flg) {
cout << "Yes" << endl;
for (int i = 1; i <= n; i++) {
cout << a[i];
if (i != n) cout << ' ';
}
cout << endl;
} else {
cout << "No" << endl;
}
return 0;
}