题意:给定两个数 n 和 k,问你用 k 个不同的质数组成 n,有多少方法。

析:dp[i][j] 表示 n 由 j 个不同的质数组成,然后先打表素数,然后就easy了。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #define print(a) printf("%d\n", (a))
  17. #define freopenr freopen("in.txt", "r", stdin)
  18. #define freopenw freopen("out.txt", "w", stdout)
  19. using namespace std;
  20. typedef long long LL;
  21. typedef pair<int, int> P;
  22. const int INF = 0x3f3f3f3f;
  23. const double inf = 0x3f3f3f3f3f3f;
  24. const LL LNF = 0x3f3f3f3f3f3f;
  25. const double PI = acos(-1.0);
  26. const double eps = 1e-8;
  27. const int maxn = 1120 + 5;
  28. const int mod = 1e9 + 7;
  29. const int dr[] = {-1, 0, 1, 0};
  30. const int dc[] = {0, 1, 0, -1};
  31. const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  32. int n, m;
  33. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  34. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  35. inline int Min(int a, int b){ return a < b ? a : b; }
  36. inline int Max(int a, int b){ return a > b ? a : b; }
  37. inline LL Min(LL a, LL b){ return a < b ? a : b; }
  38. inline LL Max(LL a, LL b){ return a > b ? a : b; }
  39. inline bool is_in(int r, int c){
  40. return r >= 0 && r < n && c >= 0 && c < m;
  41. }
  42.  
  43. int a[maxn];
  44. vector<int> prime;
  45. int dp[maxn][15];
  46.  
  47. int main(){
  48. memset(a, 0, sizeof(a));
  49. m = (int)sqrt(maxn + 0.5);
  50. for(int i = 2; i <= m; i++) if(!a[i])
  51. for(int j = i * i; j < maxn; j += i) a[j] = 1;
  52. for(int i = 2; i < maxn; ++i) if(!a[i]) prime.push_back(i);
  53. memset(dp, 0, sizeof dp);
  54.  
  55. dp[0][0] = 1;
  56. for(int k = 0; k < prime.size(); ++k){
  57. for(int i = 1120; i >= prime[k]; --i){
  58. for(int j = 1; j <= 14; ++j)
  59. dp[i][j] += dp[i-prime[k]][j-1];
  60. }
  61. }
  62.  
  63. while(scanf("%d %d", &n, &m) == 2 && m+n) printf("%d\n", dp[n][m]);
  64. return 0;
  65. }

UVa 1213 Sum of Different Primes (DP)的更多相关文章

  1. UVA 1213 Sum of Different Primes(经典dp)

    题意:选择k(k<15)个唯一质数,求出和为n(n<1121)的可能数 题解:预处理dp,dp[k][n]表示使用k个素数拼成n的总方案数 就是三重枚举,枚举k,枚举n,枚举小于n的素数 ...

  2. UVA - 1213 Sum of Different Primes (不同素数之和)(dp)

    题意:选择k个质数,使它们的和等于n,问有多少种方案. 分析:dp[i][j],选择j个质数,使它们的和等于i的方法数. #pragma comment(linker, "/STACK:10 ...

  3. UVA 1213 Sum of Different Primes

    https://vjudge.net/problem/UVA-1213 dp[i][j][k] 前i个质数里选j个和为k的方案数 枚举第i个选不选转移 #include<cstdio> # ...

  4. UVA 1213 - Sum of Different Primes(递推)

    类似一个背包问题的计数问题.(虽然我也不记得这叫什么背包了 一开始我想的状态定义是:f[n = 和为n][k 个素数]. 递推式呼之欲出: f[n][k] = sigma f[n-pi][k-1]. ...

  5. POJ 3132 &amp; ZOJ 2822 Sum of Different Primes(dp)

    题目链接: POJ:id=3132">http://poj.org/problem?id=3132 ZOJ:http://acm.zju.edu.cn/onlinejudge/show ...

  6. UVA 10163 Storage Keepers(两次DP)

    UVA 10163 Storage Keepers(两次DP) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Ite ...

  7. uva 11584 Partitioning by Palindromes 线性dp

    // uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...

  8. UVA - 825Walking on the Safe Side(dp)

    id=19217">称号: UVA - 825Walking on the Safe Side(dp) 题目大意:给出一个n * m的矩阵.起点是1 * 1,终点是n * m.这个矩阵 ...

  9. UVa 1213 (01背包变形) Sum of Different Primes

    题意: 选择K个质数使它们的和为N,求总的方案数. 分析: 虽然知道推出来了转移方程, 但还是没把代码敲出来,可能基本功还是不够吧. d(i, j)表示i个素数的和为j的方案数,则 d(i, j) = ...

随机推荐

  1. hdu4115:Eliminate the Conflict

    n<=10000局剪刀石头布,对面第i局出Ai,m<=10000种对你出什么提出的要求:Xi Yi Wi 表示第Xi局和第Yi局,Wi=1:必须不同:Wi=0:必须相同,问是否存在你一局都 ...

  2. 某考试 T1 至危警告

    题目大意就是: 设f(x)为x各个位数字之和,求x属于[0,k]且b * f(x)^a + c = x的x个数并升序输出. (a<=5  .  b,c,<=10^4  .   k<= ...

  3. 210 Course ScheduleII

    /* * 210 Course ScheduleII * 2016-6-9 by Mingyang * http://www.jyuan92.com/blog/leetcode-course-sche ...

  4. eclipse菜单字体乱码的解决

    方法一: 这个跟活动控制台代码页有关. 如果要更改为 UTF-8,则需要运行 chcp 命令: chcp 65001 有时新安装的系统可能在运行一些中文软件时显示错乱,可通过控制面板修改系统区域来管理 ...

  5. javax/servlet/ServletContext : Unsupported major.minor version 51.0

    原文:http://blog.csdn.net/mlin_123/article/details/50738532 解决:将版本从 3.1.0 改为 3.0.1 <!-- 添加servlet A ...

  6. eclipse环境下无法创建android virtual Devices(AVD)问题解决的方法汇总

    首先,要在eclipse环境下成功的创建一个安卓虚拟机,须要有三项东西,第一就是eclipse,第二就是android SDK Manager,第三就是ADT,也就是eclipse环境下的一个安卓虚拟 ...

  7. [转]图解eclipse 查看原始类出现The jar file rt.jar has no source attachment

    原文:http://blog.csdn.net/u011514810/article/details/53196371 ---------------------------------------- ...

  8. 限制input的输入类型

    1.只能输入和粘贴汉字 <input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste= ...

  9. Dropbox电面面经

    他家电面有2轮,等待onsite.. . 电面1: 国人MM面的.这点感觉非常难得. 统计近期5分钟的点击量,实现hit和getHit两个函数.这题是他家高频题,我用deque实现的,hit的均摊时间 ...

  10. AngularJS自己定义标签加入回调函数eval()

    function helloworld(name){ console.log("hello!!!!!"+name) } var name="zhangsan"; ...