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说 ...
随机推荐
- [terry笔记]Oracle会话追踪(一):SQL_TRACE&EVENT 10046
SQL_TRACE/10046 事件是 Oracle 提供的用于进行 SQL 跟踪的手段,在日常的数据库问题诊断和解决中是非常常用的方法.但其生成的trace文件需要tkprof工具生成一个可供人 ...
- Test,Nginx Hello World Module
ngx_addon_name=ngx_http_mytest_module HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module" ...
- ios开发笔记
@IBDesignable 可在第二视图中实时预览 @IBInspectable 可编辑属性
- 基于socket的客户端和服务端聊天机器人
服务端代码如下: using System;using System.Net;using System.Net.Sockets;using System.Text;using System.Threa ...
- 一段高质量的SQL从问问题开始(笔记)
首先SQL书写的目的是为了解决问题,因此只有明白了要解决的问题,才能写出更加高效的SQL语句,才能优雅的解决问题,获得更多的快乐! 在写一个SQL语句的时候不妨像优化器一样思考,问自己以下的这些问题, ...
- 九度oj 1530 最长不重复子串
原题链接:http://ac.jobdu.com/problem.php?pid=1530 字符串简单题,看似O(n^2)的复杂度10000的数据量会tle,其实最长不重复子串不超过26个嘛... 如 ...
- asp.net实现手机号码归属地查询
protected void Button1_Click(object sender, EventArgs e) { if (Regex.IsMatch(TextB ...
- wpa_supplicant软件架构分析
wpa_supplicant软件架构分析 1. 启动命令 wpa supplicant 在启动时,启动命令可以带有很多参数,目前我们的启动命令如下: wpa_supplicant /system/bi ...
- MyEclipse查看Struts2源码及Javadoc文档
一.查看Struts2源码 1.Referenced Libraries >struts2-core-2.1.6.jar>右击>properties. 2.Java Source A ...
- UISwitch swift
// // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...