题意:选择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. MAC Matlab 中文乱码

    环境:macOS High Sierra 10.13.4 问题:文件中文注释乱码(再次打开文件时) / 控制台输出中文乱码 解决方法: 官网下载补丁(https://ww2.mathworks.cn/ ...

  2. Linux系统使用ss命令查看端口状态

    Linux系统使用ss命令查看端口状态 目录 1.可用工具 2.ss帮助 2.1 选项分类说明 2.2 过滤选项family 2.3 过滤选项state 2.4 状态之间的关系 3.ss的使用 3.1 ...

  3. 085、Java数组之实现排序

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  4. 1-6SpringBoot之事务管理@Transactional

    以前学ssh ssm都有事务管理service层通过applicationContext.xml配置,所有service方法都加上事务操作: 用来保证一致性,即service方法里的多个dao操作,要 ...

  5. Java读取压缩文件信息

    不解压压缩文件,获取其中包含的文件,通过文件名检查是否包含非法文件.(后续再根据文件头或内容吧) zip: import java.util.zip.ZipEntry;import java.util ...

  6. POJ 2386 Lake Counting 八方向棋盘搜索

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53301   Accepted: 26062 D ...

  7. 最新获取 QQ头像 和 昵称接口

    网上找来的测试可用... 获取QQ头像 http://q2.qlogo.cn/headimg_dl?bs=QQ号&dst_uin=QQ号&dst_uin=QQ号&;dst_ui ...

  8. Windows和linux命令行快捷键

    Powershell的快捷键和cmd,linux中的shell,都比较像. ALT+F7 清除命令的历史记录 PgUp PgDn 显示当前会话的第一个命令和最后一个命令 Enter 执行当前命令 En ...

  9. 洛谷 P1929 迷之阶梯

    题目传送门 解题思路: f[i]表示跳到第i层的最少移动次数,如果可以从下面一级跳上来,那么直接跳上来,如果跳不上来,那就往后退,退到不能退或能跳上第i层 AC代码: #include<iost ...

  10. vue :src 不显示的解决方案

    一定要将静态资源引入 [ require("@/assets/") ],绑定到 模型绑定的:src 数据中 动态的数据才能有效   <template>   <d ...