D. Little Pony and Harmony Chest
time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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

Output the key — sequence bi that minimizes the sum described above. If there are multiple optimal sequences, you can output any of them.

Sample test(s)
input
5
1 1 1 1 1
output
1 1 1 1 1 
input
5
1 6 4 2 8
output
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 ;
}
#include <bits/stdc++.h>
#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的更多相关文章

  1. Codeforces Round #259 (Div. 2)AB

    链接:http://codeforces.com/contest/454/problem/A A. Little Pony and Crystal Mine time limit per test 1 ...

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

  3. Codeforces Round #259 (Div. 2)

    A. Little Pony and Crystal Mine 水题,每行D的个数为1,3.......n-2,n,n-2,.....3,1,然后打印即可 #include <iostream& ...

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

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

  5. 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)的是 ...

  6. Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum

    题目链接 题意:一个m个面的骰子,抛掷n次,求这n次里最大值的期望是多少.(看样例就知道) 分析: m个面抛n次的总的情况是m^n, 开始m==1时,只有一种 现在增加m = 2,  则这些情况是新增 ...

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

  8. Codeforces Round #259 (Div. 1)A(公式)

    传送门 题意 给出m个面的骰子扔n次,取最大值,求期望 分析 暴力算会有重复,而且复杂度不对. 考虑m个面扔n次得到m的概率,发现只要减去(m-1)个面扔n次得到m-1的概率即可,给出example说 ...

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

随机推荐

  1. egrep命令的实现 分类: 编译原理 2014-06-01 23:41 329人阅读 评论(0) 收藏

    本程序实现了egrep命令,首先将正则表达式转换为NFA,并实现模拟NFA的算法. 本程序使用flex实现词法分析,bison实现语法分析 若给定的一行字符串中存在一个字串能被该NFA接受,则输出整行 ...

  2. 用.NetReactor保护您的源码[转][修改]

    原文链接 前言 VS开发的源代码安全性,是很多开发者头痛的事情.于是保护好源代码便成了开发者们最关心的事情之一了. 在网上搜一搜,很多有不少的第三方工具可以为源代码加密.加密方式不外乎就是混淆,加壳. ...

  3. js和 php 介绍

    转 1. 在公司项目的改造当中,经常会遇到js与php的函数互调的情况,而实际上JS与php的设计者是不提倡这两种语言直接进行调用的,一个是客户端语言,一个服务端语言,两者之间的交互往往靠的是ajax ...

  4. ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第四天(非原创)

    文章大纲 一.课程介绍二.今日内容介绍三.参考资料下载四.参考文章 一.课程介绍 一共14天课程(1)第一天:电商行业的背景.淘淘商城的介绍.搭建项目工程.Svn的使用.(2)第二天:框架的整合.后台 ...

  5. vue项目中安装cnpm和node_modules

    1.安装cnpm的nodejs包管理工具,命令行: npm install -g cnpm --registry=https://registry.npm.taobao.org   2. 每个vue项 ...

  6. Android开发中使用代码删除数据库

    更多信息参考:Android开发中使用代码删除数据库 在Android开发中,如果用到数据库,就会有一个很麻烦的问题,就是有时候需要删除数据库很麻烦,要打开Android Device Monitor ...

  7. android ListView 分析(一)

    需要了解的内容 1. listview中的getItemAtPosition与Adapter的getItem的position的区别          listView中的getItemAtPosit ...

  8. sql语句中截取字符串

    今天在开发过程中因为要用到合并单元格,在程序里实现了以后,查出来的数据太长,都把格式撑大了,后来想想可以在sql语句查询的时候就截取,就去网上找了一下,挺好用,就转了过来: 合并单元格: /// &l ...

  9. eval()将json 字符串转换为数组

    json ={ GW:[{id:"655",mc:"董事"},{id:"656",mc:"书记"},{id:" ...

  10. CentOS 7 挂载ntfs磁盘格式的U盘

    因为CentOS 默认不识别NTFS的磁盘格式,所以我们要借助另外一个软件来挂载,那就是ntfs-3g了 自带的yum源没有这个软件,要用第三方的软件源,这里我用的是阿里的epel. 1. 切换到系统 ...