Sum of Different Primes
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3360   Accepted: 2092

Description

A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integers n and k, you should count the number of ways to express n as a sum of k different primes. Here, two ways are considered to be the same if they sum up the same set of the primes. For example, 8 can be expressed as 3 + 5 and 5 + 3 but the are not distinguished.

When n and k are 24 and 3 respectively, the answer is two because there are two sets {2, 3, 19} and {2, 5, 17} whose sums are equal to 24. There are not other sets of three primes that sum up to 24. For n = 24 and k = 2, the answer is three, because there are three sets {5, 19}, {7, 17} and {11, 13}. For n = 2 and k = 1, the answer is one, because there is only one set {2} whose sum is 2. For n = 1 and k = 1, the answer is zero. As 1 is not a prime, you shouldn’t count {1}. For n = 4 and k = 2, the answer is zero, because there are no sets of two different primes whose sums are 4.

Your job is to write a program that reports the number of such ways for the given n and k.

Input

The input is a sequence of datasets followed by a line containing two zeros separated by a space. A dataset is a line containing two positive integers n and k separated by a space. You may assume that n ≤ 1120 and k ≤ 14.

Output

The output should be composed of lines, each corresponding to an input dataset. An output line should contain one non-negative integer indicating the number of the ways for n and k specified in the corresponding dataset. You may assume that it is less than 231.

Sample Input

24 3
24 2
2 1
1 1
4 2
18 3
17 1
17 3
17 4
100 5
1000 10
1120 14
0 0

Sample Output

2
3
1
0
0
2
1
0
1
55
200102899
2079324314 思路:prim[]为素数表;
   f[i][j]为j拆分成i个素数和的方案数(1<=i&&i<=14,prim[i]<=j&&j<=1199) 边界f[0][0]=1;
   int num 为prim[]的表长;
   使用DP计算k个不同素数的和为n的方案总数:
      枚举prim[]中的prim[i](0<=i&&i<=num);
        按递减顺序枚举素数的个数j(14>=j&&j>=1);
           递减枚举前j个素数的和p(1199>=p&&p>=prim[i]);
              累计prim[i]作为第j个素数的方案总数f[j][p]+=f[j-1][p-prim[i]];
      
   f[k][n]即为解!!!!!!
 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define PI 3.141592653589792128462643383279502
#define N 1200
int prim[N]={,},f[][N],t;
int prime(){
int t=,i,j,flag;
for(i=;i<N;i+=){
for(j=,flag=;prim[j]*prim[j]<=i;j++)
if(i%prim[j]==) flag=;
if(flag){
prim[t++]=i;
}
}
return t-;
}
void s(){
for(int i=;i<=t;i++){
for(int j=;j>=;j--){
for(int p=;p>=prim[i];p--)
f[j][p]+=f[j-][p-prim[i]];
}
}
}
int main(){
//#ifdef CDZSC_June
//freopen("in.txt","r",stdin);
//#endif
//std::ios::sync_with_stdio(false);
t=prime();
int k,n;
while(scanf("%d%d",&n,&k)){
memset(f,,sizeof(f));
f[][]=;
if(k==&&n==) break;
s();
cout<<f[k][n]<<endl;
}
return ;
}

poj 3132的更多相关文章

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

  2. POJ 3132 DP+素数筛

    Sum of Different Primes Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3684   Accepted ...

  3. {POJ}{动态规划}{题目列表}

    动态规划与贪心相关: {HDU}{4739}{Zhuge Liang's Mines}{压缩DP} 题意:给定20个点坐标,求最多有多少个不相交(点也不相交)的正方形 思路:背包问题,求出所有的正方形 ...

  4. poj上的dp专题

    更新中... http://poj.org/problem?id=1037 dp[i][j][0]表示序列长度为i,以j开始并且前两位下降的合法序列数目; dp[i][j][1]表示序列长度为i, 以 ...

  5. POJ 1694 An Old Stone Game【递归+排序】

    链接: http://poj.org/problem?id=1694 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...

  6. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  7. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  8. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  9. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

随机推荐

  1. 【Foreign】画方框 [主席树]

    画方框 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output 输出一行一个整数,表示 CD 最多可能画了几个方框. Sam ...

  2. 【Atcoder】ARC084 Small Multiple

    [题意]求一个k的倍数使其数位和最小,输出数位和,k<=10^5. [算法]最短路 [题解]考虑极端情况数字是可能爆long long的(例如k*num=100...000),所以确定基本方向是 ...

  3. 【NOIP】提高组2012 vigenere密码

    [算法]模拟 #include<cstdio> #include<cstring> ; char sm[maxm],key[maxm],s[maxm]; int len,len ...

  4. rabbitmq之配置文件详解(二)

    前言 前面介绍了erlang环境的安装和rabbitmq环境安装,接下来对rabbitmq详细配置: 设置配置文件 rabbitmq的系统配置文件一般是rabbitmq.conf,可以登录后台查看它的 ...

  5. 【EverydaySport】健身笔记——背部训练

    背部训练大致可以分为两种. 1 下拉式动作 躯干纵向上下位移的动作 典型代表 这样的下拉类动作 针对的是背阔肌 也就是两边像翅膀一样的部分 2 垂直于躯干的方向作用 向内拉 主要针对的是,背部的中部 ...

  6. libSVM笔记之(一)在matlab环境下安装配置libSVM

    本文为原创作品,转载请注明出处 欢迎关注我的博客:http://blog.csdn.net/hit2015spring和http://www.cnblogs.com/xujianqing 台湾林智仁教 ...

  7. 查看linux 下进程运行时间(转)

    原文地址:http://blog.csdn.net/tspangle/article/details/11731317 可通过ps 来查看,通过参数 -o 来查看 如: ps -eo pid,tty, ...

  8. JavaScript跨域解决方法大全

    跨域的定义:JavaScript出于安全性考虑,同源策略机制对跨域访问做了限制.域仅仅是通过“URL的首部”字符串进行识别,“URL的首部”指window.location.protocol +win ...

  9. WPS2019体验

    不久之前WPS2019发布了, 说实话, 做的真的不错. 没找到2016版本多得吓人的广告, 没有那糟糕的页面设计, 没有那卡顿的体验. 而且不同的程序(文字, 演示)做成了类似标签页的形式, 体验比 ...

  10. 《java并发编程实战》读书笔记5--任务执行, Executor框架

    第6章 任务执行 6.1 在线程中执行任务 第一步要找出清晰的任务边界.大多数服务器应用程序都提供了一种自然的任务边界选择方式:以独立的请求为边界. -6.6.1 串行地执行任务 最简单的任务调度策略 ...