[CF453B]Little Pony and Harmony Chest
[CF453B]Little Pony and Harmony Chest
题目大意:
给你一个长度为\(n(n\le100)\)的正整数序列\(A(A_i\le30)\),求一个正整数序列\(B\),使得\(\sum_{i=1}^n |A_i-B_i|\)最小,且\(B\)中所有互质。
思路:
由于\(B_i\ge59\)时用\(1\)代替时不会更差,因此我们只需要考虑\(B_i\le58\)的情况。由于质因数只有\(16\)个,因此可以状压DP。另外记录转移即可。
源代码:
#include<cstdio>
#include<cctype>
#include<climits>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
const int p[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53};
const int N=101,P=16,B=59,S=1<<P;
int a[N],b[N],s[B],f[N][1<<P],g[N][1<<P];
inline void upd(int &a,const int &b) {
a=std::min(a,b);
}
int main() {
const int n=getint();
for(register int i=2;i<B;i++) {
for(register int j=0;j<P;j++) {
if(i%p[j]==0) s[i]|=1<<j;
}
}
for(register int i=1;i<=n;i++) {
a[i]=getint();
}
for(register int i=1;i<=n;i++) {
std::fill(&f[i][0],&f[i][S],INT_MAX);
for(register int j=0;j<S;j++) {
for(register int k=1;k<B;k++) {
if(j&s[k]) continue;
int &x=f[i][j|s[k]],y=f[i-1][j]+std::abs(a[i]-k);
if(y<x) {
x=y;
g[i][j|s[k]]=k;
}
}
}
}
int last=0;
for(register int i=0;i<S;i++) {
if(f[n][i]<f[n][last]) last=i;
}
for(register int i=n;i>=1;i--) {
b[i]=g[i][last];
last^=s[g[i][last]];
}
for(register int i=1;i<=n;i++) {
printf("%d%c",b[i]," \n"[i==n]);
}
return 0;
}
[CF453B]Little Pony and Harmony Chest的更多相关文章
- 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 H ...
- Codeforces 4538 (状态压缩dp)Little Pony and Harmony Chest
Little Pony and Harmony Chest 经典状态压缩dp #include <cstdio> #include <cstring> #include < ...
- 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 ...
- Codeforces 454D - Little Pony and Harmony Chest
454D - Little Pony and Harmony Chest 思路: 状压dp,由于1的时候肯定满足题意,而ai最大是30,所以只要大于等于59都可以用1替换,所以答案在1到59之间 然后 ...
- CF 435B Little Pony and Harmony Chest
Little Pony and Harmony Chest 题解: 因为 1 <= ai <= 30 所以 1 <= bi <= 58, 因为 59 和 1 等效, 所以不需 ...
- M - Little Pony and Harmony Chest 状压dp
M - Little Pony and Harmony Chest 怎么感觉自己越来越傻了,都知道状态的定义了还没有推出转移方程. 首先这个a的范围是0~30 这里可以推出 b数组的范围 0~60 ...
- Codeforces Round #259 (Div. 2)-D. Little Pony and Harmony Chest
题目范围给的很小,所以有状压的方向. 我们是构造出一个数列,且数列中每两个数的最大公约数为1; 给的A[I]<=30,这是一个突破点. 可以发现B[I]中的数不会很大,要不然就不满足,所以B[I ...
- 【CF】259 Div.1 B Little Pony and Harmony Chest
还蛮有趣的一道状态DP的题目. /* 435B */ #include <iostream> #include <string> #include <map> #i ...
- Codeforces 453B Little Pony and Harmony Chest:状压dp【记录转移路径】
题目链接:http://codeforces.com/problemset/problem/453/B 题意: 给你一个长度为n的数列a,让你构造一个长度为n的数列b. 在保证b中任意两数gcd都为1 ...
随机推荐
- 论文阅读笔记十三:The One Hundred Layers Tiramisu: Fully Convolutional DenseNets for Semantic Segmentation(FC-DenseNets)(CVPR2016)
论文链接:https://arxiv.org/pdf/1611.09326.pdf tensorflow代码:https://github.com/HasnainRaz/FC-DenseNet-Ten ...
- mybatis 遍历map;
mybatis 遍历map; 参考http://blog.csdn.net/hj7jay/article/details/78652050 ps: ${m[key]}这是显示 打印的key读value ...
- 中软酒店管理系统CSHIS操作手册_数据结构_数据字典
https://wenku.baidu.com/view/f6ca11f5ee06eff9aef807cb.html
- Caused by: java.lang.ClassNotFoundException: Didn't find class "io.grpc.helloworldexample.HelloworldActivity" on path: DexPathList
FAQ: Android app 编译好后安装到手机,运行时闪退,报如下错误: java.lang.RuntimeException: Unable to instantiate activity ...
- 一脸懵逼学习hadoop之HDFS的java客户端编写
1:eclipse创建一个项目,然后导入对应的jar包: 鼠标右击项目,点击properties或者alt+enter快捷键--->java build path--->libraries ...
- lua生成UUID
百度云盘地址: https://pan.baidu.com/s/1zo__vSeDGaHOvCJLyzHwvA GitHub地址: https://github.com/bungle/lua-rest ...
- 重排DL
题解: https://www.luogu.org/problemnew/show/T51442 从这题上还是学到不少东西.. 以前并没有写过ex-bsgs 正好拿这个复习中国剩余定理和bsgs了(我 ...
- cactiEZ 配置
CactiEZ 中文版是简单有效的cacti中文解决方案,它基于centos6 整合了cacti的相关软件,重新编译的一个新的操作系统 它基于centos6,启动速度快,支持EXT4文件系统,全中文页 ...
- 从入门到深入FIDDLER 2
在开发的过程中使用过不少的HTTP网络抓包工具,如:HTTPAnalyzer,HttpWatch. Fiddler几乎囊括了大部分的抓包请求,当然最给力的还是它的断点调试功能,尤其还有使用本地文件代替 ...
- Codeforces Round #487 (Div. 2) 跌分有感
又掉分了 这次的笑话多了. 首先,由于CF昨天的比赛太早了,忘记了有个ER,比赛开始半个小时才发现. 于是只能今天了. 嗯哈. 今天这场也算挺早的. 嗯嗯,首先打开A题. 草草看了一遍题意,以为不是自 ...