Sum of Different Primes——素数表+dp
来源:互联网 发布:管家婆软件多少钱 编辑:程序博客网 时间:2024/06/06 05:07
Think:
1题意:输入n(n <= 1120), k(k <= 14),询问由k个不同素数相加得到n的方案数,{2, 4},{4, 2}属于一种方案,输入0, 0表示结束,不作处理
2思路:素数表+dp
3反思:
1>dp状态转移方程不理解,需要进行专题练习或知识复习
2>注意状态转移方程中重复元素情况的处理
vjudge题目链接
建议参考博客
以下为Accepted代码
#include <bits/stdc++.h>using namespace std;int v[1400], dp[1400][24];void Init();int main(){ Init(); int n, k; while(scanf("%d %d", &n, &k) && (n || k)){ printf("%d\n", dp[n][k]); } return 0;}void Init(){ memset(v, 0, sizeof(v)); memset(dp, 0, sizeof(dp)); int i, j, k; v[1] = 1, v[2] = 0; for(i = 2; i <= 1400; i++){ if(!v[i]){ for(j = i*2; j <= 1400; j += i){ v[j] = 1; } } } dp[0][0] = 1; for(i = 2; i <= 1120; i++){ if(!v[i]){ for(j = 14; j >= 1; j--){ for(k = 1120; k >= i; k--){ dp[k][j] += dp[k-i][j-1]; } } } }}
阅读全文
1 0
- Sum of Different Primes——素数表+dp
- UVA1213 Sum of Different Primes(素数打表+dp)
- UVa1213 - Sum of Different Primes(素数表+DP)
- POJ3132——Sum of Different Primes
- 习题10-5 UVA - 1213 Sum of Different Primes 不同的素数之和(DP + 素数打表)
- DP Sum of Different Primes POJ 3132
- 1213 - Sum of Different Primes(DP)
- uva 1213 Sum of Different Primes, dp
- Sum of Different Primes
- POJ 3132 & ZOJ 2822 Sum of Different Primes(dp)
- UVa1213 Sum of Different Primes
- poj3132 Sum of Different Primes
- poj3132 Sum of Different Primes
- Sum of Different Primes UVA
- WOJ1274-Sum of Different Primes
- Sum of Different Primes UVA
- POJ 3132 / ZOJ 2822 : Sum of Different Primes - 动态规划,01背包,素数筛
- POJ 3132 Sum of Different Primes
- 搜索总结
- 淘淘商城系列——全局异常处理
- 每天学一点Swift----面向对象下(五)
- Java swing 实现angrybird
- PAT (Advanced Level) Practise 1110 Complete Binary Tree (25)
- Sum of Different Primes——素数表+dp
- Java_log2000_框架概念介绍
- 前端之页面加速
- noauth authentication required redis
- cookie技术
- SpringMVC DispatcherServlet重写、自定义拦截器拦截器(二)
- 进程与线程的区别关系
- Hadoop中Text类型 与 String的区别
- 气的不行之STL应用-B