[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 ...
随机推荐
- selenium 无法启动IE
解决办法是IE选项设置的安全页中,4个区域的启用保护模式的勾选都去掉(或都勾上)
- Inflated 3D ConvNet 【I3D】
Two-Stream Inflated 3D ConvNet (I3D) HMDB-51: 80.9% and UCF-101: 98.0% 在Inception-v1 Kinetics上预训练 Co ...
- 关于sql和MySQL的语句执行顺序
sql和mysql执行顺序,发现内部机制是一样的.最大区别是在别名的引用上. 一.sql执行顺序 (1) from (3) join (2) on (4) where (5) group by(开始使 ...
- mysql inner jion多表查询
select vtiger_users.id, vtiger_users.user_name, vtiger_role.rolename FROM vtiger_users inner join vt ...
- [转]PO BO VO DTO POJO DAO概念及其作用(附转换图)
来源:http://www.blogjava.net/vip01/archive/2013/05/25/92430.html J2EE开发中大量的专业缩略语很是让人迷惑,尤其是跟一些高手讨论问题的时候 ...
- XXE (XML External Entity Injection) 外部实体注入漏洞案例分析
ENTITY 实体 在一个甚至多个XML文档中频繁使用某一条数据,我们可以预先定义一个这条数据的“别名”,即一个ENTITY,然后在这些文档中需要该数据的地方调用它. XML定义了两种类型的ENTIT ...
- form表单利用iframe高仿ajax
html代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- Codeforces 264C Choosing Balls 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/CF264C.html 题目传送门 - CF264C 题意 给定一个有 $n$ 个元素的序列,序列的每一个元素是个 ...
- Calendar抽象类的使用
Calendar timeNow = Calendar.getInstance(); int year = timeNow.get(Calendar.YEAR); // 这里月是从0开始的,即0到11 ...
- 如何让你的数据有null
2018-11-13 09:25:17 如何让你的数据有null 返回时null属性不显示: String str = JSONObject.toJSONString(obj); 返回为null属 ...