CF453B Little Pony and Harmony Chest (状压DP)

CF453B CF454D

Codeforces Round #259 (Div. 2) D

Codeforces Round #259 (Div. 1) B

D. Little Pony and Harmony Chest
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements of Harmony.

A sequence of positive integers bi is harmony if and only if for every two elements of the sequence their greatest common divisor equals 1. According to an ancient book, the key of the chest is a harmony sequence bi which minimizes the following expression:

You are given sequence ai, help Princess Twilight to find the key.

Input

The first line contains an integer n (1 ≤ n ≤ 100) — the number of elements of the sequences a and b. The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 30).

Output

Output the key — sequence bi that minimizes the sum described above. If there are multiple optimal sequences, you can output any of them.

Sample test(s)
Input
5
1 1 1 1 1
Output
1 1 1 1 1 
Input
5
1 6 4 2 8
Output
1 5 3 1 8 

题意:给出n个元素组成的a[i],求b[i],要求b[i]每两个元素互质(最大公约数为1),使各个i的abs(b[i]-a[i])的和最小。n<=100,a[i]<=30

题解:状压DP。

可以发现其实我们就用这么几个数,因为1是可以无限用的,最差我们就用1,a[i]=30的时候,距离1和59一样近,所以我们b[i]就从1~59里面选。

然后要互质,就是要两个数没有相同的约数,可以当做两个数不是同一个质数的倍数。这样我们把某个数的质因数标记上,另一个数的质因数要是和这个数的质因数有重合,就不行。

然后1~59之间,质数只有17个,可以用1<<17种状态表示已经用了哪些质数当因数。这样我们就能用超碉的状态压缩DP了。

f[i][j]  搞完第i个数,状态为j的最小消耗。初始化为INF,f[0][0]为0。
g[i][j]  记这个状态b[i]放的是什么数
pre[i][j]  记上个状态是什么

 

途中会多次用到某个数的质因数们,可以先算好st[]存1~59的质因数的状态。

若(st[l]&j)为真,说明j这个状态后面不能加l这个数了……

 

代码:

 1 //#pragma comment(linker, "/STACK:102400000,102400000")
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<cmath>
 8 #include<map>
 9 #include<set>
10 #include<stack>
11 #include<queue>
12 using namespace std;
13 #define ll long long
14 #define usll unsigned ll
15 #define mz(array) memset(array, 0, sizeof(array))
16 #define minf(array) memset(array, 0x3f, sizeof(array))
17 #define REP(i,n) for(i=0;i<(n);i++)
18 #define FOR(i,x,n) for(i=(x);i<=(n);i++)
19 #define RD(x) scanf("%d",&x)
20 #define RD2(x,y) scanf("%d%d",&x,&y)
21 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
22 #define WN(x) prllf("%d\n",x);
23 #define RE  freopen("D.in","r",stdin)
24 #define WE  freopen("1biao.out","w",stdout)
25 #define mp make_pair
26 #define pb push_back
27 const int maxj=1<<17;
28 const int inf=0x3f3f3f3f;
29 const int pr[18]= {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,666},pn=17;
30 int isp[60]= {1};
31 int f[111][maxj];///f[i][j] 搞完第i个数,状态为j的最小消耗
32 int g[111][maxj];///记这个状态b[i]放的是什么数
33 int pre[111][maxj];///记上个状态是什么
34 
35 int st[60];
36 
37 int n;
38 int a[111];
39 int main() {
40     int i,j,k,l;
41     mz(isp);
42     REP(i,pn) isp[pr[i]]=1;
43     for(k=1; k<=59; k++) {
44         st[k]=0;
45         for(l=0; pr[l]<=k; l++) {
46             if(k%pr[l]==0) st[k]|=(1<<l);
47         }
48     }
49 
50     scanf("%d",&n);
51     REP(i,n)scanf("%d",&a[i]);
52     mz(g);
53     mz(pre);
54     memset(f,0x3f,sizeof(f));
55     f[0][0]=0;
56     for(i=0; i<n; i++) {
57         for(j=0; j<maxj; j++) {
58             if(f[i][j]!=inf) {
59                 for(k=1; k<=59; k++) {
60                     int x=st[k];
61                     if(x&j)continue;
62                     x=x|j;
63                     int biu=f[i][j] + abs(k-a[i]);
64                     if(biu<f[i+1][x]) {
65                         //printf("%d %d %d %x\n",i,j,k,x);
66                         f[i+1][x]=biu;
67                         g[i+1][x]=k;
68                         pre[i+1][x]=j;
69                     }
70                 }
71             }
72         }
73     }
74     l=0;
75     for(j=1; j<maxj; j++) {
76         if(f[n][j]<f[n][l])l=j;
77     }
78     vector<int>ans;
79     ans.clear();
80     for(i=n; i>0; i--) {
81         ans.pb(g[i][l]);
82         l=pre[i][l];
83     }
84     j=ans.size();
85     printf("%d",ans[j-1]);
86     for(i=j-2; i>=0; i--)
87         printf(" %d",ans[i]);
88     puts("");
89     return 0;
90 }
View Code

 

posted @ 2014-08-20 19:34  带鱼Yuiffy  阅读(379)  评论(0编辑  收藏  举报