[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. C++ LocalAlloc() & LocalSize() & LocalFree ()

    关于LocalAlloc function,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/aa366723(v=vs.85). ...

  2. spring cloud 声明式rest客户端feign调用远程http服务

    在Spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.Feign就是Spring Cloud提供的一种声明式R ...

  3. 饮冰三年-人工智能-Python-13Python基础之运算符与数据类型

    1:算数运算符 + - * / ** % // 2: 成员运算符 in not in name = """张三""" if "张& ...

  4. 步步为营-90-SEO(url重写+超链接技巧)

    目的:便于搜索引擎抓取 url重写:将带参数的url如:https://i.cnblogs.com/EditPosts.aspx?opt=1.修改为https://i.cnblogs.com/Edit ...

  5. asp.net core WebApi 返回 HttpResponseMessage

    ASP.NET WebApi 2 中的示例代码: [Route("values/{id}")] public async Task<HttpResponseMessage&g ...

  6. 构造函数与 new 命令

    虽然不同于传统的面向对象编程语言,但是JavaScript具有很强的面向对象编程能力.本章介绍JavaScript如何进行“面向对象编程”. 对象的概念 “面向对象编程”(Object Oriente ...

  7. UE4 日志

    第一种 输出在控制台中,需要在启动之后的游戏窗口中点击~以打开控制台,然后输入showlog,这时候会弹出一个cmd日志窗口.在程序中使用代码 UE_LOG(LogTemp, Warning, TEX ...

  8. Java集合源码学习(二)ArrayList

    1.关于ArrayList ArrayList直接继承AbstractList,实现了List. RandomAccess.Cloneable.Serializable接口,为什么叫"Arr ...

  9. Linux 的文件类型

    Linux 的文件通常分为 7 大类 文件类型                                  缩写      英文名称                                ...

  10. [转]Centos 查看端口占用情况和开启端口命令

    http://www.cnblogs.com/xqzt/p/4919191.html 1.Centos 查看端口占用 比如查看 80 端口占用情况使用如下命令: lsof -i tcp:80 2.列出 ...