习题10-5 UVA - 1213 Sum of Different Primes 不同的素数之和(DP + 素数打表)
来源:互联网 发布:手机淘宝怎么进闲鱼 编辑:程序博客网 时间:2024/05/22 09:49
大体题意:
选择K个质数,使他们的和等于N ,问有多少种选法!
思路:
N不大 小于等于1120
直接打一个素数表,然后dp
dp[i][j] 表示 选择j 个素数 构成i 方法数。
三层循环 第一层直接枚举素数, 第二层枚举选择的数量,第三层枚举构成的结果N。
#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;const int maxn = 2000 + 5;int dp[maxn][maxn/100];int vis[maxn]; // 0 is prime!void init(){ int m = sqrt(maxn + 0.5); for (int i = 2; i <= maxn; ++i) if (!vis[i]) for (int j = i * i; j <= maxn; j += i) vis[j] = 1; dp[0][0] = 1; vis[0] = vis[1] = 1; for (int i = 0; i <= maxn; ++i){ if (vis[i])continue; for (int j = 14; j >= 1; --j){ for (int k = maxn; k >= i; --k){ dp[k][j] += dp[k-i][j-1]; } } }}int main(){ init(); int n,k; while(scanf("%d%d",&n,&k) == 2 && (n||k)) printf("%d\n",dp[n][k]); return 0;}
0 0
- 习题10-5 UVA - 1213 Sum of Different Primes 不同的素数之和(DP + 素数打表)
- UVA1213 Sum of Different Primes(素数打表+dp)
- UVa1213 - Sum of Different Primes(素数表+DP)
- Sum of Different Primes——素数表+dp
- 习题10-5 不同素数之和 uva 1213
- uva 1213 Sum of Different Primes, dp
- 习题10-6 UVA - 1210 Sum of Consecutive Prime Numbers 连续素数之和(滑动窗口)
- 1213 - Sum of Different Primes(DP)
- 习题10-5 不同素数之和 UVa1213
- uva 1213 不同素数之和
- UVA 1213 Sum of Different Primes
- UVA 1213 Sum of Different Primes
- UVA 1213(p343)----Sum of Different Primes
- Sum of Different Primes UVA
- Sum of Different Primes UVA
- UVA 1210 Sum of Consecutive Prime Numbers(素数打表)
- 习题10-5 连续素数之和 uva 1210
- UVa Problem 10168 Summation of Four Primes (四素数之和)
- C++ IPv4与IPv6的兼容编码
- Hadoop 之 Shuffle and Sort
- android唯一机器码生成方案
- Node.js、Express、Socket.io 入门
- --Notepad++快捷键 单行、多行、区块注释
- 习题10-5 UVA - 1213 Sum of Different Primes 不同的素数之和(DP + 素数打表)
- 2016"百度之星"-资格赛-1001-A
- CentOS7搭建CI(GitLab + Gerrit + Jenkins)系统
- 第十、十一周项目一:阅读程序,并写出运行结果(1)
- iOS 多线程篇5—GCD介绍
- Android Launcher分析和修改1——Launcher默认界面配置(default_workspace)
- 软件开发活动
- 必看关于AndroidMainfest.xml,androidmainfest详解、总结,概括了解
- ubuntu 下安装metasploit