454D - Little Pony and Harmony Chest

思路:

状压dp,由于1的时候肯定满足题意,而ai最大是30,所以只要大于等于59都可以用1替换,所以答案在1到59之间

然后筛出1到58之间的质数,只有16个,把1到58的数的状态由这16个质数表示,如果整除这个质数则二进制中这一位为1,否则则为0

状态:dp[i][j]表示到第i个数为止选取的数的状态为j的最小差和

初始状态:dp[0][0]=0

状态转移:

dp[i+1][j|sta[k]]=min(dp[i+1][j|sta[k]],dp[i][j]+abs(k-a[i+1]))(j&sta[k]==0,1<=k<=58)

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) const int INF=0x3f3f3f3f;
int sta[];
int a[];
int res[];
int dp[][(<<)+];
int ans[][(<<)+];
int prime[]={,,,,,,,,,,,,,,,};
int getsta(int x){
int ans=;
for(int i=;i<;i++){
if(x%prime[i]==)ans|=<<i;
}
return ans;
}
void dfs(int n,int s){
if(n==)return ;
res[n]=ans[n][s];
dfs(n-,s^sta[res[n]]);
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
for(int i=;i<;i++){
sta[i]=getsta(i);
}
int n;
cin>>n;
for(int i=;i<=n;i++)cin>>a[i];
mem(dp,INF);
mem(ans,-);
dp[][]=ans[][]=;
for(int i=;i<n;i++){
for(int j=;j<(<<);j++){
if(ans[i][j]==-)continue;
for(int k=;k<;k++){
if(j&sta[k])continue;
int s=j|sta[k];
if(ans[i+][s]==-||dp[i][j]+abs(k-a[i+])<dp[i+][s]){
dp[i+][s]=dp[i][j]+abs(k-a[i+]);
ans[i+][s]=k;
}
}
}
}
int mn=INF,s=;
for(int i=;i<(<<);i++){
if(dp[n][i]<mn){
mn=dp[n][i];
s=i;
}
}
dfs(n,s);
for(int i=;i<=n;i++)cout<<res[i]<<" ";
cout<<endl;
return ;
}

Codeforces 454D - Little Pony and Harmony Chest的更多相关文章

  1. Codeforces 453B Little Pony and Harmony Chest:状压dp【记录转移路径】

    题目链接:http://codeforces.com/problemset/problem/453/B 题意: 给你一个长度为n的数列a,让你构造一个长度为n的数列b. 在保证b中任意两数gcd都为1 ...

  2. Codeforces 4538 (状态压缩dp)Little Pony and Harmony Chest

    Little Pony and Harmony Chest 经典状态压缩dp #include <cstdio> #include <cstring> #include < ...

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

  4. CF453B Little Pony and Harmony Chest (状压DP)

    CF453B CF454D Codeforces Round #259 (Div. 2) D Codeforces Round #259 (Div. 1) B D. Little Pony and H ...

  5. [CF453B]Little Pony and Harmony Chest

    [CF453B]Little Pony and Harmony Chest 题目大意: 给你一个长度为\(n(n\le100)\)的正整数序列\(A(A_i\le30)\),求一个正整数序列\(B\) ...

  6. CF 435B Little Pony and Harmony Chest

    Little Pony and Harmony Chest 题解: 因为 1 <= ai <= 30 所以  1 <= bi <= 58, 因为 59 和 1 等效, 所以不需 ...

  7. M - Little Pony and Harmony Chest 状压dp

    M - Little Pony and Harmony Chest 怎么感觉自己越来越傻了,都知道状态的定义了还没有推出转移方程. 首先这个a的范围是0~30   这里可以推出 b数组的范围 0~60 ...

  8. codeforces 454 D. Little Pony and Harmony Chest(状压dp)

    题目链接:http://codeforces.com/contest/454/problem/D 题意:给定一个序列a, 求一序列b,要求∑|ai−bi|最小.并且b中任意两数的最大公约数为1. 题解 ...

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

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

随机推荐

  1. html05

    1.js中的对象-内置对象-外部对象-自定义对象 2.常见的内置对象有哪些?-String对象-Number对象-Boolean对象-Array对象-Math对象-Date对象-RegExp正则对象- ...

  2. centos 专题-各种配置应有尽有

    你想要的在这里都能得到解决: http://www.linuxidc.com/topicnews.aspx?tid=14

  3. Summary: difference between public, default, protected, and private key words

    According to Java Tutorial: Controlling Access to Members of a Class Access level modifiers determin ...

  4. 把 ElasticSearch 当成是 NoSQL 数据库

    Elasticsearch 可以被当成一个 “NoSQL”-数据库来使用么? NoSQL 意味着在不同的环境下存在不同的东西, 而erestingly 它并不是真的跟 SQL 有啥关系. 我们开始只会 ...

  5. mongodb mongotemplate聚合

    1.group by并且计算总数 @Test public void insertTest() { //测试数据 //insertTestData(); Aggregation agg = Aggre ...

  6. zw版【转发·台湾nvp系列Delphi例程】HALCON ZoomImageFactor2

    zw版[转发·台湾nvp系列Delphi例程]HALCON ZoomImageFactor2 procedure TForm1.Button1Click(Sender: TObject);var op ...

  7. Q-learning简明实例

    本文是对 http://mnemstudio.org/path-finding-q-learning-tutorial.htm 的翻译,共分两部分,第一部分为中文翻译,第二部分为英文原文.翻译时为方便 ...

  8. 20165207 Exp2 后门原理与实践

    20165207 Exp2 后门原理与实践 〇.实验准备 两个虚拟机,一个kali一个win7.kali的ip是192.168.43.72,win7的ip是192.168.43.116,在win7关掉 ...

  9. Nginx 灰度实现方式(支持纯灰度,纯生产,50度灰及更多比例配置)

    前言 Nginx相关技术短信本篇幅不做详细介绍,所以学习本文之前要对Nginx有相关的了解. 生产环境即线上环境,在经历开发.测试再到上线,不可避免的会更新生产环境,但谁又能保证测试过的代码到线上运行 ...

  10. monit

    Monit 是一个用来监测系统状态的工具,不但可以用来监视进程.服务.文件.目录.文件系统,还可以在服务 down 掉的时候自动重启服务或者当某个进程占用过多的资源的时候自动停掉进程,并且支持 Ema ...