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 ;
}

最新文章

  1. java中可定制的序列化过程 writeObject与readObject
  2. 一道javascript数组操作题
  3. JS valueOf与字符串
  4. Ubuntu gmake: command not found
  5. [Node.js] 也说this
  6. Java-Junit 的Hello world
  7. SKTexture类
  8. Sending forms through JavaScript
  9. BZOJ_2049_[Sdoi2008]Cave 洞穴勘测_LCT
  10. 【工作查漏补缺】jQuery ajax - serializeArray()
  11. 如何使用桥接模式使虚拟机VMware中的Redhat能上网
  12. debian apache2 多端口对应多文件 虚拟端口配置
  13. java执行post请求,并获取json结果组成想要的内容存放本地txt中
  14. 设计模式-行为型模式,python备忘录模式
  15. Django中的Templates
  16. 开启linux远程访问权限
  17. linux,添加新硬盘的方法
  18. Visual Studio的Debugger Visualizers
  19. pygame经典sprite精灵类
  20. maven学习--生命周期

热门文章

  1. redis.conf详细说明
  2. 【BZOJ】1574: [Usaco2009 Jan]地震损坏Damage
  3. javascript继承机制 &amp; call apply使用说明
  4. IE浏览器Bug总结
  5. mysql中的单引号/小数点/字符转换为数字/警告信息
  6. windows下常用快捷键(转)
  7. Django-【template】自定义过滤器和自定义标签
  8. 我用.htaccess做了些什么?
  9. map,set的底层实现:红黑树[多图,手机慎入]
  10. python内建方法