Codeforces Round #259 (Div. 2) D. Little Pony and Harmony Chest 状压DP
2015-10-06 21:25
561 查看
D. Little Pony and Harmony Chest
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.
题意:
给你一个长度不超过100的数组A, 求数组B使得对应元素差值的和最小,且B数组两两元素互质。
很容易看出来,B[i]最大值不会超过58, 59也可以但是用59的话为什么不用1呢。
用mask[i]表示i的质因子,比如mask[6] = 3
dp[i][mask[j]|k] = min(dp[i-1][j] + abs(A[i] - k)) 表示前i个数中已经选了质因子的状态为mask[j]|k, 转移的时候要保证mask[j] & k == 0,
然后记录转移的过程(为了输出B),然后就完了
相关文章推荐
- HDU 4850 Wow! Such String! 欧拉回路
- i/o notes
- ubuntu系统与win 7系统时间不一致问题
- Python正则表达式学习小结
- 拿什么拯救你,“闯祸”的充电宝
- leetcode16
- UIKit学习笔记1
- 充电宝质量良莠不齐 请谨慎选择
- 最近公共祖先(LCA):并查集+深搜
- 一个PHP开发者总结的九条建议
- UI 动画之CALayer+CAAnimation 之 CAKeyframeAnimation
- c++primer plus第十七章-cin输入、cout输出和文件I/O
- 设计模式之ChainOfResponsibility:过滤Request和Response
- 结对项目博客
- 实现一个简单的散列表(HashMap)
- 96 c 段错误 && int *p = NULL 和 *p = NULL区别
- 【转】46 个非常有用的 PHP 代码片段
- 汇编学习笔记
- 隐私保护——笔记
- Java基础知识强化之集合框架笔记45:Set集合之TreeSet存储自定义对象并遍历练习1(自然排序)