Codeforces Round #259 (Div. 2)-D. Little Pony and Harmony Chest
题目范围给的很小,所以有状压的方向。
我们是构造出一个数列,且数列中每两个数的最大公约数为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的更多相关文章
- 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. 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) 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, 则这些情况是新增 ...
- 【CF】259 Div.1 B Little Pony and Harmony Chest
还蛮有趣的一道状态DP的题目. /* 435B */ #include <iostream> #include <string> #include <map> #i ...
- 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 ...
- 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. 2)
A. Little Pony and Crystal Mine 水题,每行D的个数为1,3.......n-2,n,n-2,.....3,1,然后打印即可 #include <iostream& ...
- Codeforces Round #259 (Div. 1)A(公式)
传送门 题意 给出m个面的骰子扔n次,取最大值,求期望 分析 暴力算会有重复,而且复杂度不对. 考虑m个面扔n次得到m的概率,发现只要减去(m-1)个面扔n次得到m-1的概率即可,给出example说 ...
随机推荐
- Android中内容观察者的使用---- ContentObserver类详解
详解:http://blog.csdn.net/qinjuning/article/details/7047607
- Oracle 分区表的统计信息实例
ORACLE的统计信息在执行SQL的过程中扮演着非常重要的作用,而且ORACLE在表的各个层次都会有不同的统计信息,通过这些统计信息来描述表的,列的各种各样的统计信息.下面通过一个复合分区表来说明一些 ...
- 009-python基础-数据类型-列表和元组
一.列表 在python中叫"列表",其他语言中成为"数组" 元素中可以存储字符串.数字甚至变量. 元素索引顺序从0开始. 例如 name_list[0] 就是 ...
- Python学习教程(learning Python)--2.1 Python下自定义函数
在Python里可以自定义函数,实现某特定功能,这里首先要区分一下函数的定义和函数的调用两个基本概念,初学者往往容易混淆. 函数的定义是指将一堆能实现特定功能的语句用一个函数名标识起来,而函数的调用则 ...
- GROUPING SETS、CUBE、ROLLUP
其实还是写一个Demo 比较好 USE tempdb IF OBJECT_ID( 'dbo.T1' , 'U' )IS NOT NULL BEGIN DROP TABLE dbo.T1; END; G ...
- Java入门到精通——基础篇之static关键字
一.概述 static 关键字是声明静态变量,静态方法用的.static的含义是属于类且不属于类对象的变量和函数. 二.static的产生. 在创建对象的时候除非用new ...
- SQL30081N 检测到通信错误。正在使用的通信协议:"TCP/IP"
环境描述: 今天在虚拟机上安装了Linux系统,并且装了DB2,但是在连接的时候遇到了个问题,百思不得其解.下面是具体问题跟解决办法 问题描述: 解决办法: 1.先ping服务器是否可以ping通. ...
- hdu 3549 Flow Problem
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Description Network flow is a well- ...
- oracle12c不能进入到http://localhost:5500/em的解决办法
Oracle11g企业管理器无法打开——解决https://localhost:1158/em 页面无法打开的问题 常见的问题:https://localhost:1158/em 无法打开 解决办法: ...
- [转]ubuntu server上网配置
[转]ubuntu server上网配置 http://blog.sina.com.cn/s/blog_6c9d65a101011pyt.html 今天我的ubuntu server上不去网了,所以重 ...