SGU116 动态规划 DP
2014-01-29 22:54
381 查看
问题:超级质数是在质数序列中下标也是质数的质数,例如3,5,11……现在给你一个数,找一个由超级质数构成的最小表示使得这些数的和等于这个数。
解法:先求出这些超级质数,然后就是个完全背包问题。具体可见我动态规划关于背包的文章。
Problem: Super-prime number is such a prime number that its current number in prime numbers sequence is a prime number too. For example, 3,5,11...Your task is to find index of super-prime for given numbers and find optimal
presentation as a sum of super-primes.
Solution: At first, calculate all the super-prime you need, then it comes into a complete knapsack problem. You can find more about the topic in my articles.
解法:先求出这些超级质数,然后就是个完全背包问题。具体可见我动态规划关于背包的文章。
Problem: Super-prime number is such a prime number that its current number in prime numbers sequence is a prime number too. For example, 3,5,11...Your task is to find index of super-prime for given numbers and find optimal
presentation as a sum of super-primes.
Solution: At first, calculate all the super-prime you need, then it comes into a complete knapsack problem. You can find more about the topic in my articles.
#include <stdio.h> #include <iostream> #include <cstring> #include <algorithm> using namespace std; #define inf 1010001000 int cnt,pnum,p[10000],q[10000],n,dp[10100],his[10100],k,ans[10100]; bool f[10010]; void output(int x) { if (x<=0) return; ans[++k] = x-his[x]; output(his[x]); } int main() { pnum = 0; memset(f,0,sizeof(f)); for (int i=2;i<=10000;i++) if (!f[i]) { p[++pnum] = i; for (int j=i;j<=10000;j+=i) f[j] = true; } cnt = 0; for (int i=1;i<=pnum;i++) if (p[i]<=pnum) q[++cnt] = p[p[i]]; while (~scanf("%d",&n)) { for (int i=1;i<=10010;i++) dp[i] = -inf; dp[0] = 0; for (int i=1;i<=cnt;i++) for (int j=q[i];j<=n;j++) if (dp[j]<dp[j-q[i]]-1) { dp[j] = dp[j-q[i]]-1; his[j] = j-q[i]; } if (dp ==-inf) printf("0\n"); else { printf("%d\n",-dp ); k = 0; output(n); sort(ans+1,ans+k+1); for (int i=k;i>1;i--) printf("%d ",ans[i]); printf("%d\n",ans[1]); } } return 0; }
相关文章推荐
- SGU116 动态规划 DP
- 【动态规划】最长公子序列 - dp
- 动态规划(dp)讲解
- leetcode 327. Count of Range Sum 字段和问题 + 动态规划DP解决 + 字段数 + 暴力求解真好
- DP 动态规划 Problem O 1015 二进制十进制转换
- 动态规划入门(三)DP 基本思想 具体实现 经典题目 POJ1159 POJ1458 POJ1141
- Communication System dp 动态规划 || 贪心
- poj 动态规划DP - 1163 The Triangle
- hdu1864(最大报销额 DP动态规划)
- poj 动态规划DP - 1664 放苹果
- openjudge 拦截导弹 (DP 动态规划)
- To the Max(动态规划DP)
- hdu_1078_FatMouse and Cheese_神奇的贪心_动态规划_算不上是dp
- 最长上升子序列 动态规划 dp
- DP算法(Dynamic Programming,俗称动态规划)是最经典算法之一
- 【NOIP】CODE[VS] 1044 拦截导弹 动态规划 序列型DP
- 夕拾算法进阶篇:28)矩阵连乘(动态规划DP)
- POJ 1458 Common Subsequence (DP 动态规划)
- HDU2059 龟兔赛跑 动态规划 DP