[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 ...
随机推荐
- idea 设置选中代码得背景颜色
- spring集成jwt验证方式,token验证
为什么要告别session?有这样一个场景,系统的数据量达到千万级,需要几台服务器部署,当一个用户在其中一台服务器登录后,用session保存其登录信息,其他服务器怎么知道该用户登录了?(单点登录), ...
- 专注笔试算法20年(C语言版)
1.C语言实现链表数据的反转({1,2,3,4}->{4,3,2,1}). int trav(PNode *head){ PNode p_1,p_2,tmp; //判断参数是否有效 if(*he ...
- Ajax爬虫必用到的字典转换器
1.使用情景 在我们Ajax爬虫时需要用到以下这样的数据的时候我们会一个一个地复制粘贴,这样会很麻烦 def dictionary_converter(key_value): '''主要用于爬虫时复制 ...
- 【C++ Primer | 11】关联容器(一)
在multimap或multiset中查找元素 第二种方法解释: #include <iostream> #include <utility> #include <ite ...
- 处理OSS上传失败一例
问题描述: 上传oss云存储,无法成功,报错: 2019-01-29 17:44:59,552 oss2.api [ERROR] 140080008111872 : Exception: {'stat ...
- gradle repo conf - maven-central.storage-download.googleapis.com
repositories { google() jcenter() maven { // The google mirror is less flaky than mavenCentral() url ...
- java 解析域名得到host
// 形如https://www.baidu.com 或 www.baidu.com, 判断这两种情况,并解析前者去掉http头,传入domain host // 方案1:正则表达式 + URI解析方 ...
- Ubuntu下Gradle环境配置
sudo gedit ~/.profile sudo source ~/.profile env # for java export JAVA_HOME=/home/cmm/jdk export CL ...
- Quartz.net入门
简介 Quartz.NET是一个开源的作业调度框架,是OpenSymphony的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不 ...