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这个数了……

代码:

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) prllf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
#define pb push_back
const int maxj=<<;
const int inf=0x3f3f3f3f;
const int pr[]= {,,,,,,,,,,,,,,,,,},pn=;
int isp[]= {};
int f[][maxj];///f[i][j] 搞完第i个数,状态为j的最小消耗
int g[][maxj];///记这个状态b[i]放的是什么数
int pre[][maxj];///记上个状态是什么 int st[]; int n;
int a[];
int main() {
int i,j,k,l;
mz(isp);
REP(i,pn) isp[pr[i]]=;
for(k=; k<=; k++) {
st[k]=;
for(l=; pr[l]<=k; l++) {
if(k%pr[l]==) st[k]|=(<<l);
}
} scanf("%d",&n);
REP(i,n)scanf("%d",&a[i]);
mz(g);
mz(pre);
memset(f,0x3f,sizeof(f));
f[][]=;
for(i=; i<n; i++) {
for(j=; j<maxj; j++) {
if(f[i][j]!=inf) {
for(k=; k<=; k++) {
int x=st[k];
if(x&j)continue;
x=x|j;
int biu=f[i][j] + abs(k-a[i]);
if(biu<f[i+][x]) {
//printf("%d %d %d %x\n",i,j,k,x);
f[i+][x]=biu;
g[i+][x]=k;
pre[i+][x]=j;
}
}
}
}
}
l=;
for(j=; j<maxj; j++) {
if(f[n][j]<f[n][l])l=j;
}
vector<int>ans;
ans.clear();
for(i=n; i>; i--) {
ans.pb(g[i][l]);
l=pre[i][l];
}
j=ans.size();
printf("%d",ans[j-]);
for(i=j-; i>=; i--)
printf(" %d",ans[i]);
puts("");
return ;
}

CF453B Little Pony and Harmony Chest (状压DP)的更多相关文章

  1. M - Little Pony and Harmony Chest 状压dp

    M - Little Pony and Harmony Chest 怎么感觉自己越来越傻了,都知道状态的定义了还没有推出转移方程. 首先这个a的范围是0~30   这里可以推出 b数组的范围 0~60 ...

  2. Codeforces Round #259 (Div. 2) D. Little Pony and Harmony Chest 状压DP

    D. Little Pony and Harmony Chest   Princess Twilight went to Celestia and Luna's old castle to resea ...

  3. [CF453B]Little Pony and Harmony Chest

    [CF453B]Little Pony and Harmony Chest 题目大意: 给你一个长度为\(n(n\le100)\)的正整数序列\(A(A_i\le30)\),求一个正整数序列\(B\) ...

  4. Codeforces 453B Little Pony and Harmony Chest:状压dp【记录转移路径】

    题目链接:http://codeforces.com/problemset/problem/453/B 题意: 给你一个长度为n的数列a,让你构造一个长度为n的数列b. 在保证b中任意两数gcd都为1 ...

  5. codeforces 454 D. Little Pony and Harmony Chest(状压dp)

    题目链接:http://codeforces.com/contest/454/problem/D 题意:给定一个序列a, 求一序列b,要求∑|ai−bi|最小.并且b中任意两数的最大公约数为1. 题解 ...

  6. Codeforces 454D - Little Pony and Harmony Chest

    454D - Little Pony and Harmony Chest 思路: 状压dp,由于1的时候肯定满足题意,而ai最大是30,所以只要大于等于59都可以用1替换,所以答案在1到59之间 然后 ...

  7. CF 435B Little Pony and Harmony Chest

    Little Pony and Harmony Chest 题解: 因为 1 <= ai <= 30 所以  1 <= bi <= 58, 因为 59 和 1 等效, 所以不需 ...

  8. Codeforces 4538 (状态压缩dp)Little Pony and Harmony Chest

    Little Pony and Harmony Chest 经典状态压缩dp #include <cstdio> #include <cstring> #include < ...

  9. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

随机推荐

  1. 【codevs1993】 草地排水

    http://codevs.cn/problem/1993/ (题目链接) 题意 求有向图最大流. Solution Dinic. 代码 // codevs1993 #include<algor ...

  2. Linux WebServer WebRoot Path Identification

    目录 . HTTPD(Apache) . NGINX . TENGINE . JBOSS . TOMCAT . LIGHTTPD 1. HTTPD(Apache) 0x1: 启动参数 Usage: . ...

  3. git命令笔记

    git -bare init git remote add origin ssh://myserver.com/home/git/myapp.git git remote show origin [r ...

  4. Beta版本冲刺第六天 12.12

    一.站立式会议照片: 二.项目燃尽图: Android端 后台 三.项目进展: 成 员 昨天完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 邮箱验证和用户评价的填写 用户评价的查看以 ...

  5. hdu–2369 Bone Collector II(01背包变形题)

    题意:求解01背包价值的第K优解. 分析: 基本思想是将每个状态都表示成有序队列,将状态转移方程中的max/min转化成有序队列的合并. 首先看01背包求最优解的状态转移方程:\[dp\left[ j ...

  6. ApkTool反编译apk,去除广告或者汉化后重新打包apk,并签名

    http://my.oschina.net/mopidick/blog/135581 以ubuntu的环境为例子,window的大同小异 首先下载apktool工具:https://code.goog ...

  7. Linux DHCP通过OPTION43为H3C的AP下发AC地址

    对于DHCP服务,可以在很多平台上进行设置.那么这里我们就主要讲解一下在Linux DHCP服务器上通过option 43实现H3C的AP自动联系AC注册的相关内容.原来的DHCP Server是放在 ...

  8. python增删改查(四)

    ###增删改查 names = ["zhangding","wangxu","wudong","cheng"] #增 n ...

  9. 【转】七种常见阈值分割代码(Otsu、最大熵、迭代法、自适应阀值、手动、迭代法、基本全局阈值法)

    http://blog.csdn.net/xw20084898/article/details/17564957 一.工具:VC+OpenCV 二.语言:C++ 三.原理 otsu法(最大类间方差法, ...

  10. Yocto开发笔记之《Makefile编写》(QQ交流群:519230208)

    开了一个交流群,欢迎爱好者和开发者一起交流,转载请注明出处. QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 =============================== ...