题意:选择k个质数,使它们的和等于n,问有多少种方案。

分析:dp[i][j],选择j个质数,使它们的和等于i的方法数。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b) {
if(fabs(a - b) < eps) return 0;
return a < b ? -1 : 1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1120 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int dp[MAXN][20];
int vis[MAXN];
void init(){
vis[0] = vis[1] = 1;
for(int i = 2; i <= sqrt(MAXN + 0.5); ++i){
if(!vis[i]){
for(int j = i * i; j < MAXN; j += i){
vis[j] = 1;
}
}
}
dp[0][0] = 1;
for(int i = 0; i < MAXN; ++i){
if(vis[i]) continue;
for(int j = 14; j >= 1; --j){
for(int k = MAXN - 1; k >= i; --k){
dp[k][j] += dp[k - i][j - 1];
}
}
}
}
int main(){
init();
int n, k;
while(scanf("%d%d", &n, &k) == 2){
if(!n && !k) return 0;
printf("%d\n", dp[n][k]);
}
return 0;
}

  

UVA - 1213 Sum of Different Primes (不同素数之和)(dp)的更多相关文章

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

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

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

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

  3. UVa 1213 Sum of Different Primes (DP)

    题意:给定两个数 n 和 k,问你用 k 个不同的质数组成 n,有多少方法. 析:dp[i][j] 表示 n 由 j 个不同的质数组成,然后先打表素数,然后就easy了. 代码如下: #pragma ...

  4. UVA 1213 Sum of Different Primes

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

  5. UVa 1210 连续素数之和

    https://vjudge.net/problem/UVA-1210 题意: 输入整数n,有多少种方案可以把n写成若干个连续素数之和? 思路: 先素数打表,然后求个前缀和. #include< ...

  6. 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 ...

  7. sicily 1259. Sum of Consecutive Primes

    Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...

  8. codeforces 569C C. Primes or Palindromes?(素数筛+dp)

    题目链接: C. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes in ...

  9. POJ 2739 Sum of Consecutive Prime Numbers(素数)

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

随机推荐

  1. SPring整合Mybatis方式一

    Spring整合Mybatis 需要maven包: mysql-connector-java 5.1.47, mybatis 3.5.2, spring-webmvc 5.2.2.RELEASE, s ...

  2. Masonry与UITableView+FDTemplateLayoutCell搭配使用

    打个小广告:本人开发了一个宠物相关的App,欢迎大家下载体验~ 下载二维码: 进入正文: 之前发过一篇博客,也是对这两个的练习使用,但是之后遇到些问题,所以删除重写了.抱歉 Masonry是一款轻量级 ...

  3. Linux打印变量、环境配置、别名和文件删除操作

    一.打印命令 1.echo打印命令 a.打印环境变量 echo $Path b.打印Path命令目录 which,比如:which ls表示打印的是Path目录中第一定义的全局变量的目录中命令. 二. ...

  4. NO33 第6--7关题目讲解

    客户端(电脑)通过浏览器输入域名,先找hosts文件及本地dns缓存,若都没有,就找localDNS服务器,若没有,localDNF服务器找根服务器(全球13台的那个根”.“服务器),根就把.com这 ...

  5. Docker + Maven + Docker-compose

    前言: docker:容器化管理 maven:支持docker-maven的插件,通过 mvn clean -Dmaven.test.skip package dockerfile:build 打包命 ...

  6. vSphere HA 原理与配置

    内容预览: 1. vSphere HA 概述 2. vSphere HA 提供的保护级别 3. vSphere HA运行原理 4. vSphere HA 故障支持场景 5. vSphere HA接入控 ...

  7. mitmproxy 配置

    pip install mitmproxy Man In The Middle 原理 mitmproxy工程工具包,主要包含了3个组件 功能一致,交互界面不同 mitmproxy:命令行界面,wind ...

  8. 012.Delphi插件之QPlugins,多实例内嵌窗口服务

    这个DEMO中主要是在DLL中建立了一个IDockableControl类,并在DLL的子类中写了具体的实现方法. 在主程序exe中,找到这个服务,然后调用DLL的内嵌方法,把DLL插件窗口内嵌到主程 ...

  9. JavaScript的调用

    1 方法调用模式 var myObject = { value : 0, increment : function(inc) { alert('hi'); } }; myObject.incremen ...

  10. HDU - 6130 Kolakoski (打表)

    题意:由1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1,……合并可得1,22,11,2,1,22,1,22,11,2,11,22,1,再由每个数的位数可得新序列,推出新 ...