[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的更多相关文章

  1. 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 ...

  2. Codeforces 4538 (状态压缩dp)Little Pony and Harmony Chest

    Little Pony and Harmony Chest 经典状态压缩dp #include <cstdio> #include <cstring> #include < ...

  3. 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 ...

  4. Codeforces 454D - Little Pony and Harmony Chest

    454D - Little Pony and Harmony Chest 思路: 状压dp,由于1的时候肯定满足题意,而ai最大是30,所以只要大于等于59都可以用1替换,所以答案在1到59之间 然后 ...

  5. CF 435B Little Pony and Harmony Chest

    Little Pony and Harmony Chest 题解: 因为 1 <= ai <= 30 所以  1 <= bi <= 58, 因为 59 和 1 等效, 所以不需 ...

  6. M - Little Pony and Harmony Chest 状压dp

    M - Little Pony and Harmony Chest 怎么感觉自己越来越傻了,都知道状态的定义了还没有推出转移方程. 首先这个a的范围是0~30   这里可以推出 b数组的范围 0~60 ...

  7. Codeforces Round #259 (Div. 2)-D. Little Pony and Harmony Chest

    题目范围给的很小,所以有状压的方向. 我们是构造出一个数列,且数列中每两个数的最大公约数为1; 给的A[I]<=30,这是一个突破点. 可以发现B[I]中的数不会很大,要不然就不满足,所以B[I ...

  8. 【CF】259 Div.1 B Little Pony and Harmony Chest

    还蛮有趣的一道状态DP的题目. /* 435B */ #include <iostream> #include <string> #include <map> #i ...

  9. Codeforces 453B Little Pony and Harmony Chest:状压dp【记录转移路径】

    题目链接:http://codeforces.com/problemset/problem/453/B 题意: 给你一个长度为n的数列a,让你构造一个长度为n的数列b. 在保证b中任意两数gcd都为1 ...

随机推荐

  1. GetComputerNameEx()

    昨晚看了MSDN提供的GetComputerNameEx function(参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms ...

  2. AI-序列化-查-做接口

    序列化最终代码(下边的可以不看) from rest_framework.views import APIView from rest_framework import serializers fro ...

  3. XMind思维导图使用笔记

    首先新建一个空白的图 以组织结构图(向下)  为例 1.双击组织结构图 创建一个空白的页面 2.随便选择一个风格 这时候出现工作台 现在里面只有一个中心主题 正文部分开始 1.如果想要添加一个子主题 ...

  4. ConfigurationManager 类的使用

    一.引用 命名空间:   System.Configuration程序集:  System.Configuration(位于 System.Configuration.dll) 二.示例 1.读取.增 ...

  5. 用JAVA写一个简单的英文加密器

    package qhs; import java.util.Scanner; public class JiaM { public static void main(String[] args) { ...

  6. Java charAt() 方法

    charAt() 方法用于返回指定索引处的字符.索引范围为从 0 到 length() - 1. 参数 index -- 字符的索引. 返回值 返回指定索引处的字符. 实例 public class ...

  7. Python Day-1 练习

    作业1 要求:1.输入用户密码 2.认真成功后显示欢迎信息 3.输入三次锁定 代码如下: __author__ = 'zhang.ning' username = "zhangning&qu ...

  8. Eclipse中如何打开Map/Reduce Locations窗口

    Window->Show View->Other->MapReduce Tools,双击打开

  9. 使用shell脚本定时采集日志数据到hdfs分布式文件系统

    1.首先对linux操作系统的crontab命令进行熟悉和了解: .crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系 ...

  10. .net core vs2015 vs2017打开后errpr

    需要将每个项目中增加global.json  并设置sdk的版本,注意,每个类库中均需增加. { "sdk": { "version": "1.0.0 ...