题意:选择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. Linux 创建网卡子接口

    创建网卡子接口,添加IP别名 ifconfig eth0:0  2.2.2.2/24 或 ip addr add 2.2.2.2/24 dev eth0 label eth0:0 清除网卡子接口,删除 ...

  2. ZB本地设置

    (1)web.config <!--本地服务器--> <add name="connectionString" connectionString="mG ...

  3. JavaScript图片

    <ul> <li> <a href="images/666.jpg" title="frist img">frist< ...

  4. 转:Nginx的超时keeplive_timeout配置详解

    https://blog.csdn.net/weixin_42350212/article/details/81123932 Nginx 处理的每个请求均有相应的超时设置.如果做好这些超时时间的限定, ...

  5. 腾讯云直播生成推流链接node.js版

    /** * 获取推流地址 * 如果不传key和过期时间,将返回不含防盗链的url * @param domain 您用来推流的域名 * streamName 您用来区别不同推流地址的唯一流名称 * k ...

  6. JAVA实现单例模式的四种方法和一些特点

    JAVA实现单例模式的四种方法和一些特点,需要的朋友可以参考一下     一.饿汉式单例类 复制代码 代码如下: public class Singleton  {      private Sing ...

  7. python2学习------基础语法1 (变量、分支语句、循环语句、字符串操作)

    1.变量类型 Numbers(数字):int,float,long String(字符串) List(列表) tuple(元组) dict(字典) bool(布尔):True,False # 删除变量 ...

  8. iOS 枚举ENUM和OPTIONS的区别

  9. 面试题之xml解析?

    题目是:用java程序将xml中的数据保存到实体对象中,如何实现? xml如下: <?xml version="1.0" encoding="UTF-8" ...

  10. mysql8 安装&问题解决

    1.下载:https://dev.mysql.com/downloads/mysql/ 2.安装 1).设置环境变量 MYSQL_HOME D:\env\j2ee\mysql\mysql-8.0.19 ...