LGTB 与 序列
题目描述
输入格式
输出格式
样例
样例输入
5
1 6 4 2 8
样例输出
3
数据范围与提示
code
/*
5
1 6 4 2 8
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
const int Inf = 0x3f3f3f3f;
int prime[] = { 0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 };
bool cmp(int a, int b) { return a > b; }
int a[maxn], dp[17][1 << 17];
int jl[maxn];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1, cmp);
memset(jl, 0, sizeof(jl));
for (int i = 1; i <= 58; i++)
for (int j = 1; j <= 16; j++)
if (i < prime[j])
break;
else if (i % prime[j] == 0)
jl[i] |= (1 << j - 1);
memset(dp, 0x3f, sizeof(dp));
dp[0][0] = 0;
for (int i = 1; i <= min(n, 16); i++)
for (int j = 0; j < (1 << 16); j++)
for (int num = 1; num <= 58; num++)
if (!(jl[num] & j)) {
int s = j | jl[num];
dp[i][s] = min(dp[i][s], dp[i - 1][j] + abs(a[i] - num));
}
int ans = Inf;
for (int j = 0; j < (1 << 16); j++) ans = min(ans, dp[min(n, 16)][j]);
if (n > 16)
for (int i = 17; i <= n; i++) ans += abs(a[i] - 1);
return cout << ans, 0;
}