Codeforces Round #259 (Div. 2) D
4 seconds
256 megabytes
standard input
standard output
Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements of Harmony.
A sequence of positive integers bi is harmony if and only if for every two elements of the sequence their greatest common divisor equals 1. According to an ancient book, the key of the chest is a harmony sequence bi which minimizes the following expression:
You are given sequence ai, help Princess Twilight to find the key.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of elements of the sequences a and b. The next line contains n integersa1, a2, ..., an (1 ≤ ai ≤ 30).
Output the key — sequence bi that minimizes the sum described above. If there are multiple optimal sequences, you can output any of them.
5
1 1 1 1 1
1 1 1 1 1
5
1 6 4 2 8
1 5 3 1 8
sl : 很傻比的状态压缩,直接背包就搞了,又傻逼了。
// by caonima
// hehe
#include <bits/stdc++.h>
using namespace std;
const int MAX= ;
const int inf = 0x3f3f3f3f;
int dp[MAX][<<],val[MAX],a[MAX],ans[MAX][<<];
int prime[MAX],vis[MAX],cur=;
vector<int> C;
void init() {
memset(vis,,sizeof(vis));
vis[]=;
for(int i=;i<;i++) {
if(!vis[i]) prime[cur++]=i;
for(int j=i;j<;j+=i) vis[j]=;
}
return ;
}
int main() {
init();
int n;
while(scanf("%d",&n)==) {
memset(val,,sizeof(val));
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
}
for(int i=;i<;i++) {
for(int j=;j<cur;j++) {
if(i%prime[j]==) {
val[i]|=(<<j);
}
}
}
for(int i=;i<=n;i++) {
for(int j=;j<(<<cur);j++) dp[i][j]=inf;
}
for(int i=;i<(<<cur);i++) dp[][i]=;
for(int i=;i<=n;i++) {
for(int j=;j<(<<cur);j++) {
for(int k=;k<;k++) {
if((j&val[k])==) {
int res=dp[i-][j^val[k]]+abs(k-a[i]);
if(res<dp[i][j]) {
dp[i][j]=res;
ans[i][j]=k;
}
}
}
}
}
int res=inf ,state;
for(int i=;i<(<<cur);i++) {
if(res>dp[n][i]) {
res=dp[n][i];
state=i;
}
}
// printf("%d\n",ans[n][0]);
for(int i=n;i>=;i--) {
C.push_back(ans[i][state]);
int k=ans[i][state];
state=state^(val[k]);
}
for(int i=C.size()-;i>=;i--) {
printf("%d ",C[i]);
}
printf("\n");
}
return ;
}


#define debug() printf("sss");
using namespace std;
const int inf = 0x3f3f3f3f;
const int MAX = ;
int cur=,vis[MAX],prime[MAX],state[MAX];
vector<int> C;
int dp[MAX][<<],a[MAX],b[MAX],n,res[MAX][<<];
void init() {
for(int i=;i<MAX;i++) {
if(!vis[i]) prime[cur++]=i;
for(int j=i;j<MAX;j+=i) vis[j]=;
}
memset(state,,sizeof(state));
for(int i=;i<;i++) {
for(int j=;j<;j++) {
if(i%prime[j]==) state[i]|=(<<j);
}
}
}
int dfs(int pos,int s) {
if(pos>n) return ;
if(~dp[pos][s]) return dp[pos][s];
int ans=inf;
for(int i=;i<;i++) {
if(s&state[i]) continue;
int t=dfs(pos+,s|state[i])+abs(i-a[pos]);
if(ans>t) {
ans=t;
res[pos][s]=i;
}
}
return dp[pos][s]=ans;
}
int main() {
init();
while(scanf("%d",&n)==) {
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
}
memset(dp,-,sizeof(dp));
dfs(,);
int s=;
for(int i=;i<=n;i++) {
C.push_back(res[i][s]);
int k=res[i][s];
s=s|(state[k]);
}
for(int i=;i<C.size();i++) {
printf("%d ",C[i]);
}
printf("\n");
}
}
Codeforces Round #259 (Div. 2) D的更多相关文章
- Codeforces Round #259 (Div. 2)AB
链接:http://codeforces.com/contest/454/problem/A A. Little Pony and Crystal Mine time limit per test 1 ...
- Codeforces Round #259 (Div. 1) A. Little Pony and Expected Maximum 数学公式结论找规律水题
A. Little Pony and Expected Maximum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...
- Codeforces Round #259 (Div. 2)
A. Little Pony and Crystal Mine 水题,每行D的个数为1,3.......n-2,n,n-2,.....3,1,然后打印即可 #include <iostream& ...
- Codeforces Round #259 (Div. 2)-D. Little Pony and Harmony Chest
题目范围给的很小,所以有状压的方向. 我们是构造出一个数列,且数列中每两个数的最大公约数为1; 给的A[I]<=30,这是一个突破点. 可以发现B[I]中的数不会很大,要不然就不满足,所以B[I ...
- Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum (数学期望)
题目链接 题意 : 一个m面的骰子,掷n次,问得到最大值的期望. 思路 : 数学期望,离散时的公式是E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn) p(xi)的是 ...
- Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum
题目链接 题意:一个m个面的骰子,抛掷n次,求这n次里最大值的期望是多少.(看样例就知道) 分析: m个面抛n次的总的情况是m^n, 开始m==1时,只有一种 现在增加m = 2, 则这些情况是新增 ...
- 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 Round #259 (Div. 1)A(公式)
传送门 题意 给出m个面的骰子扔n次,取最大值,求期望 分析 暴力算会有重复,而且复杂度不对. 考虑m个面扔n次得到m的概率,发现只要减去(m-1)个面扔n次得到m-1的概率即可,给出example说 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
随机推荐
- 光盘安装ubuntu出现busybox-initramfs不能继续安装的终极解决方法
我用的是Intel G45的主板,可能是板子太新的原因,kernel支持有问题 从光盘(官方寄来的光盘)安装ubuntu,出现提示: BusyBox V1.1.3 (Debian 1:1.1.3-5u ...
- 员工管理系统(集合与IO流的结合使用 beta2.0 ObjectInputStream/ ObjectOutputStream)
package cn.employee; import java.io.Serializable; public class Employee implements Serializable{ pri ...
- redis的安装使用以及一些常用的命令
Redis是一个key-value存储系统.并提供多种语言的API,我们可使用它构建高性能,可扩展的Web应用程序.目前越来越多的网站用它来当做缓存,减轻服务器的压力. 本文安装用的到redis是绿色 ...
- 外文翻译 《How we decide》赛场上的四分卫 第二节
本书导言翻译 本章第一节 "决定是如何做出来的",关于意识最神秘的问题之一.尽管我们时刻做着决定,但是我们没有感觉到大脑内部的一系列有关进程.NFL球探挑选候选球员的评分表中,决策 ...
- Spring注解驱动开发之Ioc容器篇
前言:现今SpringBoot.SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了Spring的一些底层注解.原理,比如@Conditional.@Import.@Ena ...
- Burp Suite集成sqlmap
本地环境 JDK1.8 Burp Suite 1.7.26 Python2.7 64位安装包 sqlmap zip包 安装python及sqlmap python下载下来默认安装即可,配置系统环境变量 ...
- Uml 建模 一(类图建模和startuml的使用)
本文将分三个部分介绍Uml建模:Uml建模的作用.类图.startuml的使用 Uml的作用 本文以java为例介绍Uml,在当前的软件开发中大多数使用面向对象开发(OO),面向对象的就是将现实世界中 ...
- scala基础篇-03 if与for
一.Scala中的if是表达式** 1.定义方式 2.例子 二.for 的用法 1.定义方式: for{ x <- xs y=x+ ) }yield y 2.例子:
- [转] NTFS Permission issue with TAKEOWN & ICACLS
(转自:NTFS Permission issue with TAKEOWN & ICACLS - SAUGATA 原文日期:2013.11.19) Most of us using TA ...
- python 内置2to3工具将python2代码转换为python3代码
python2与python3代码不兼容,如果需要python2代码在python3环境下运行,需要将代码进行转换,本文介绍使用python3内置工具2to3.py对代码进行转换 一:2to3.py在 ...