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. DFS经典题,reachable or not in a 2D maze

    [[0, 0, 0, 0, 0, 1], [1, 1, 0, 0, 0, 1], [0, 0, 0, 1, 0, 0], [0, 1, 1, 0, 0, 1], [0, 1, 0, 0, 1, 0], ...

  2. 设置CentOS6.5时钟同步

    一.测试ntp服务 # rpm -q ntp ntp-4.2.4p8-2.el6.x86_64 // 这表示已安装了,如果没有安装,这是空白. 二./etc/ntp.conf 红色部分是修改的. 配置 ...

  3. Hadoop设置环境变量注意事项

    路径是/etc/profile. 这个东西不能再普通下设置,打开是彩色的,即便是“:wq!”也不能保存,必须去root下,黑白的. 然后root下source /etc/profile,然后exit, ...

  4. jquery插件-表单提交插件-jQuery.Form

    1.介绍 JQuery Form插件是一款强大的Ajax表单提交插件,可以简单方便的实现让我们的表单 由传统的提交方式转换成Ajax无刷新提交! 他提供了两个核心的方法ajaxForm以及ajaxSu ...

  5. Bzoj2763 [JLOI2011]飞行路线

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2651  Solved: 1004 Description Alice和Bob现在要乘飞机旅行,他们选 ...

  6. 帮助理解委托、匿名委托、Lambda表达式还有事件

    写了一个小程序,能够很好的认识到这三个的用法 namespace Lambda { /// <summary> /// 实现根据指定运算形式 输出结果 /// </summary&g ...

  7. 正则表达式:Pattern类与Matcher类详解

    一.捕获组的概念 捕获组可以通过从左到右计算其开括号来编号,编号是从1 开始的.例如,在表达式((A)(B(C)))中,存在四个这样的组: 1     ((A)(B(C))) 2     (A) 3  ...

  8. Beta Daily Scrum 第六天

    [目录] 1.任务进度 2.困难及解决 3.燃尽图 4.代码check-in 5.总结 1. 任务进度 学号 今日完成 明日完成 612 昨天完成任务,今天看有没有bug,找一找 好像没什么事 615 ...

  9. A/D转换实验

    实验效果:

  10. CS架构和BS架构的区别

    C/S结构,即Client/Server(客户机/服务器)结构,是大家熟知的软件系统体系结构,通过将任务合理分配到Client端和Server端,降低了系统的通讯开销,可以充分利用两端硬件环境的优势. ...