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. 【poj1085】 Triangle War

    http://poj.org/problem?id=1085 (题目链接) 题意 A,B两人玩游戏,在一个大三角形上放火柴,若A放上一根火柴后成功组成一个三角形,那么这个三角形就归属于A,并且A被奖励 ...

  2. bzoj2683简单题

    #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> ...

  3. 树莓派利用PuTTY进行远程登录

    得到树莓派的IP:192.168.199.220 打开PuTTY: 端口为22 选择SSH 点击Open: 输入账号密码:pi/raspberry(注意,在linux下输入密码是看不见的) 如果要使用 ...

  4. Jenkins实现测试环境到生产环境一键部署(Windows)

    前言: 因为dotnet在发布站点后,然后再上传服务时,会因为各种的网速问题,导致站点瞬间挂掉!那么通过一键部署,先在测试站点测试好的文件,复制到正式站点上的一个缓冲区,进行预热配置,之后再本机进行文 ...

  5. C语言的时间函数

    下面是C语言的获取本地时间和构造时间进行格式化时间显示输出的相关函数:This page is part of release 3.35 of the Linux man-pages project. ...

  6. git命令的使用 【备用】

    *下面为对本地仓库进行操作 [将文件提交到本地仓库] git add 文件名                     将文件添加到暂存区 git status                      ...

  7. Linux Overflow Vulnerability General Hardened Defense Technology、Grsecurity/PaX

    Catalog . Linux attack vector . Grsecurity/PaX . Hardened toolchain . Default addition of the Stack ...

  8. RabbitMQctl命令

    RabbitMQControl RabbitMQ提供了可视化的网页供我们进行一些配置与操作,但是ctl的命令比UI来的专业的多,一些UI无法完成的操作就需要使用ctl命令来进行处理了 这里是官方的文档 ...

  9. python实现自动输入命令回车操作

    苦逼的在sf上等了一天(问题链接),都没人来解答,只好自己想办法,东平西凑还是勉强实现了,记录一下: 安装完python2.7后,在cmd命令行输入python回车,后出现python相关的提示信息, ...

  10. TypeScript Basic Types(基本类型)

    在学习TypeScript之前,我们需要先知道怎么才能让TypeScript写的东西正确的运行起来.有两种方式:使用Visual studio 和使用 NodeJs. 这里我选择的是NodeJs来编译 ...