F1:有点复杂的做法。但是不需要脑子。
考虑如果确定了第一个数 ,寻找第二个数时,一定是贪心地选择能让 更小的。
即,假设当前前缀 为 ,需要找到尽可能小的 满足 ,且存在 满足 ,。
由于这样的计算过程 每次至少被除以 ,所以真正有效(大于 )的 不超过 个。
接着判断 的存在性。令 ,莫比乌斯反演:
令 表示数组中整除 的数的个数,则枚举 ,枚举 ,求和若大于 ,则 存在。
复杂度应该是 ,常数很小且跑不满,可以通过。
F2:注意到把最小的 放在最前最优。结论:,。证明:。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ONLINE_JUDGE
#define fi first
#define se second
#define pb push_back
#define vec vector
#define pii pair<int, int>
#define pll pair<ll, ll>
int t;
int n;
int a[100010];
vec< int > d[100010];
int f[100010];
int p[100010], isp[100010], tot, mu[100010];
void init() {
mu[1] = 1;
for(int i = 2; i <= 1e5; i ++) {
if(!isp[i]) {
p[++ tot] = i;
mu[i] = -1;
}
for(int j = 1; j <= tot && 1ll * i * p[j] <= 1e5; j ++) {
isp[i * p[j]] = 1;
mu[i * p[j]] = -mu[i];
if(i % p[j] == 0) {
mu[i * p[j]] = 0;
break;
}
}
}
}
void solve() {
cin >> n;
for(int i = 1; i <= n; i ++) cin >> a[i];
int g = 0;
for(int i = 1; i <= n; i ++) g = __gcd(a[i], g);
for(int i = 1; i <= n; i ++) a[i] /= g;
int mx = 0;
for(int i = 1; i <= n; i ++) {
mx = max(mx, a[i]);
// c[a[i]] ++;
for(int j = 1; 1ll * j * j <= a[i]; j ++) {
if(a[i] % j) continue;
f[j] ++;
if(j * j != a[i]) f[a[i] / j] ++;
}
}
// sort(a + 1, a + n + 1);
// for(int i = 1; i <= mx; i ++) cout << f[i] << '\n';
ll ans = 1e18;
for(int i = 1; i <= n; i ++) {
int lst = a[i];
ll sum = a[i];
// cout << a[i] << '\n';
for(int j = 2; j <= n; j ++) {
// cout << j << " " << lst << '\n';
if(lst == 1) {
sum += n - j + 1;
break;
}
for(auto p : d[lst]) {
int cnt = 0;
for(auto q : d[lst / p]) {
cnt += mu[q] * f[p * q];
}
// cout << lst << " " << p << " " << cnt << '\n';
if(cnt) {
lst = p;
break;
}
}
sum += lst;
}
// cout << sum << '\n';
ans = min(ans, sum);
}
// cout << g << " " << ans << '\n';
cout << ans * g << '\n';
for(int i = 1; i <= mx; i ++) f[i] = 0;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
init();
for(int i = 1; i <= 1e5; i ++) {
for(int j = i; j <= 1e5; j += i) {
d[j].pb(i);
}
}
cin >> t;
while(t --) solve();
return 0;
}