题解:CF2013E Prefix GCD
Suin_tryin · · 题解
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;
}