Little Pony and Harmony Chest

题解:

因为 1 <= ai <= 30 所以  1 <= bi <= 58, 因为 59 和 1 等效, 所以不需要59。

[1, 58]只有16个质数,对于这16个质数去状压。

对于1->58的数,我们计算出每个数对于质数来说的状态,然后转移。

代码:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 2e5 + ;
int prim[] = {,,,,,,,,,,,,,,,};
int state[];
int n;
int a[];
int dp[][<<];
int pre[][<<];
int use[][<<];
void P(int x, int v){
if(!x) return;
P(x-, pre[x][v]);
printf("%d ", use[x][v]);
}
int Ac(){
scanf("%d", &n);
for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
int tot = <<;
for(int i = ; i <= ; ++i)
for(int j = ; j < ; ++j)
if(i%prim[j] == )
state[i] |= << j;
memset(dp, inf, sizeof dp);
dp[][] = ;
for(int i = ; i < n; ++i){
for(int j = ; j < tot; ++j){
if(dp[i][j] == inf) continue;
for(int k = ; k <= ; ++k){
if(j & state[k]) continue;
int x = j | state[k];
if(dp[i+][x] > dp[i][j] + abs(a[i+] - k)){
dp[i+][x] = dp[i][j] + abs(a[i+] - k);
pre[i+][x] = j;
use[i+][x] = k;
}
}
}
}
int id = ;
for(int j = ; j < tot; ++j){
if(dp[n][id] > dp[n][j]){
id = j;
}
}
P(n, id);
return ;
} int main(){
Ac();
return ;
}
05-11 16:14