Codeforces 453B B. Little Pony and Harmony Chest(dp+数论)
来源:互联网 发布:医药b2b关键词 seo 编辑:程序博客网 时间:2024/05/21 06:50
题解 : 首先发现这个题目中的 b 数组中每一个元素都不会超过 58 为什么呢 ? 自己想去 ….
然后我们发现,两个数互质就是没有相同的素因子,然后 1 -57 只有 18 个素数,先将这 18 个数打进一个表然后,用
state[i] 存下每个数包含的所有的素因子。
剩下的就是转移了
dp[i][j] 表示前 i 个 用了 j 这些素数 (j 是一个二维的压缩状态)。
dp[i + 1][j | state[k]] = min (dp[i][j] + abs (k - a[i]),dp[i + 1][j | k]) ;
由于这个题目要求保留路径,那么就还需要开一个数组去储存路径就好了
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;const int INF = 1e9 + 7;const int maxn = (1 << 18);int prime[] = {2,3,5,7,11,13,17,19,23,29,31,37,39,41,43,47,53,57};int a[105] = {0};int b[105] = {0};int state[60] = {0};int dp[105][maxn] = {0};int pos[105][maxn] = {0};int main() { ios_base :: sync_with_stdio(false); int n; cin >> n; for (int i = 1;i <= n; ++ i) cin >> a[i]; for (int i = 2;i < 60; ++ i) { for (int j = 0;j < 18; ++ j) { if (i % prime[j] == 0) { state[i] |= (1 << j); } } } for (int i = 0;i < 105; ++ i) { for (int j = 0; j < maxn; ++ j) dp[i][j] = INF; } dp[0][0] = 0; for (int i = 0;i < n; ++ i) { for (int j = 0;j < maxn; ++ j) { if (dp[i][j] == INF) continue; if (dp[i + 1][j] > dp[i][j] + a[i + 1] - 1) { dp[i + 1][j] = dp[i][j] + a[i + 1] - 1; pos[i + 1][j] = 1; } for (int k = 2;k < 60; ++ k) { if (state[k] & j) continue; else { int u = abs (a[i + 1] - k); if (u + dp[i][j] < dp[i + 1][j | state[k]]) { dp[i + 1][j | state[k]] = dp[i][j] + u; pos[i + 1][j | state[k]] = k; } } } } } int ans = INF; int tem = 0; for (int i = 0;i < maxn; ++ i) { if (dp[n][i] < ans) { ans = dp[n][i]; tem = i; } } int cnt = 0; for (int i = n;i > 0;-- i) { b[cnt ++] = pos[i][tem]; tem -= state[pos[i][tem]]; } for (int i = cnt - 1;i >= 0 ; -- i) { cout << b[i] << ' '; } return 0;}
阅读全文
0 0
- codeforces 453B B. Little Pony and Harmony Chest(dp+数论)
- Codeforces 453B B. Little Pony and Harmony Chest(dp+数论)
- Codeforces 453B Little Pony and Harmony Chest 状压dp
- codeforces 453 B Little Pony and Harmony Chest (状压dp)
- Codeforces 453B Little Pony and Harmony Chest 状压DP
- codeforces 453 B Little Pony and Harmony Chest (状压dp)
- 【DP】 codeforces 453B Little Pony and Harmony Chest
- codeforces 453B Little Pony and Harmony Chest
- Codeforces 453 B. Little Pony and Harmony Chest
- Codeforces 453B Little Pony and Harmony Chest(状压)
- CodeForces 453B Little Pony and Harmony Chest
- codeforces 453B Little Pony and Harmony Chest (离散化+dp状态压缩)
- Codeforces Round #259 (Div. 1) B. Little Pony and Harmony Chest(状压dp)
- Codeforces 453 B Little Pony and Harmony Chest(Round 259 div.1 B/div.2 D)
- codeforce 453B - Little Pony and Harmony Chest
- CF 453B Little Pony and Harmony Chest 解题报告(状态压缩DP)
- Codeforces 454D Little Pony and Harmony Chest 状压DP
- CF 453B(Little Pony and Harmony Chest-数列最小加减1更改方案,满足任意2数互质-位运算dp+最坏情况分析+记忆化搜索)
- 使用fastboot命令刷机流程详解
- tar使用方法
- 2 矩阵消元
- 【编译器原理】编译器的工作过程
- java 关键字
- Codeforces 453B B. Little Pony and Harmony Chest(dp+数论)
- 【POI】Apache POI
- kindeditor实现回车发送,ctrl+回车换行
- Netty源码解读EventExecutorGroup
- react api笔记
- Centos7之Maven安装-yellowcong
- 4 Values whose Sum is 0 UVA
- 埃氏筛法
- 项目二