题目范围给的很小,所以有状压的方向。

我们是构造出一个数列,且数列中每两个数的最大公约数为1;

给的A[I]<=30,这是一个突破点。

可以发现B[I]中的数不会很大,要不然就不满足,所以B[I]<=60左右。构造DP方程DP[I][J]=MIN(DP[I][J],DP[I][J^C[K]]+abs(a[i]-k));

K为我们假设把这个数填进数组中的数。同时开相同空间记录位置,方便输出结果。。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int cnt=;
int c[],a[];
int dp[][(<<)+];
int pre[][(<<)+][];
int b[]={};
int p[]; void prime()//帅选素数
{
for (int i=;i<;i++)
if (!b[i])
{
p[++cnt]=i;
for (int j=i+i;j<;j+=i)
b[j]=;
}
} int cal(int x)//对每个数分解素数
{
int ans=;
for (int i=;i<=cnt;i++)
{
if (x%p[i]==) ans|=(<<(i-));
while (x%p[i]==) x/=p[i];
}
return ans;
} void print(int x,int pos)//递归出数组
{
if (x==) return;
print(x-,pre[x][pos][]);
cout<<pre[x][pos][]<<" ";
} int main()
{
int n;
prime();
cin>>n;
for (int i=;i<=n;i++) cin>>a[i];
for (int i=;i<;i++)
c[i]=cal(i);
memset(dp,inf,sizeof(dp));
memset(dp[],,sizeof(dp[]));//初始化 for (int i=;i<=n;i++)
for (int j=;j<(<<);j++)
for (int k=;k<;k++)
if ((j&c[k])==c[k]){//由前一个状态推后一个状态
int tmp=dp[i-][j^c[k]]+abs(k-a[i]);
if (dp[i][j]>tmp)
{
dp[i][j]=tmp;
pre[i][j][]=(j^c[k]);
pre[i][j][]=k;
}
} int min=inf,pos;
for (int i=;i<(<<);i++)
if (dp[n][i]<min)
{
min=dp[n][i];
pos=i;
}
print(n,pos);
return ;
}

Codeforces Round #259 (Div. 2)-D. Little Pony and Harmony Chest的更多相关文章

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

  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) C - Little Pony and Expected Maximum (数学期望)

    题目链接 题意 : 一个m面的骰子,掷n次,问得到最大值的期望. 思路 : 数学期望,离散时的公式是E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn) p(xi)的是 ...

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

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

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

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

  6. Codeforces Round #259 (Div. 2) D

    D. Little Pony and Harmony Chest time limit per test 4 seconds memory limit per test 256 megabytes i ...

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

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

  8. Codeforces Round #259 (Div. 2)

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

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

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

随机推荐

  1. DevExpress汉化(WinForm)

    /* *隔壁老王原创,2013-09-21,转载请保留本人信息及本文地址. *本文地址:http://wallimn.iteye.com/blog/1944191 */ 最简单的方式就是使用汉化资源, ...

  2. Mybatis typeAliases别名

    <typeAliases> <typeAlias type="com.green.phonemanage.model.CellPhone" alias=" ...

  3. 【转】Oracle中如何用一条SQL快速生成10万条测试数据

    转自http://blog.csdn.net/welken/article/details/4971887   做数据库开发或管理的人经常要创建大量的测试数据,动不动就需要上万条,如果一条一条的录入, ...

  4. Redbean:入门(三) - Exec 以及 Query 以及 ConvertToBeans

    <?php //引入rb入口文件 include_once 'rb.php'; //定义dsn以及相关的数据 $dsn = 'mysql:host=localhost;dbname=hwibs_ ...

  5. hdu 5249 KPI

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5249 KPI Description 你工作以后, KPI 就是你的全部了. 我开发了一个服务,取得了 ...

  6. Erlang generic standard behaviours -- gen

    在分析 gen_server (或者是gen_fsm )之前,首先应该弄明白,gen 这个module . -module(gen). -compile({inline,[get_node/1]}). ...

  7. golang:slice陷阱

    slice陷阱,slice底层指向某个array,在赋值后容易导致array长期被引用而无法释放

  8. wpa_supplicant测试

    Android系统中对于WIFI的设置集成到了“设置”中,其实跟手动设置差不多.这里介绍下如何手动连接WIFI,以方便以后调试WIFI. 第一步要做的就是要加载WIFI模块驱动了.当然如果你的WIFI ...

  9. nodejs for centos配置

    mongodb http://www.cnblogs.com/zhoulf/archive/2013/01/31/2887439.html nodejs http://zhaohe162.blog.1 ...

  10. mac中添加环境变量

    sudo vi /etc/paths 来编辑 paths,将环境变量添加到 paths 中. vim 是一个编辑器,另外还有几个,如:Pico,Emacs.