Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3473   Accepted: 2154

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

Source

 

用素数筛打一个素数表出来,然后在素数表上背包动规。

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int mxn=;
int pri[mxn],cnt=;
int vis[mxn];
int n,k;
int f[mxn][];
void Pri(){
int i,j;
for(i=;i<=n;i++){
if(!vis[i]){
pri[++cnt]=i;
}
for(j=;j<=cnt && i*pri[j]<=n;j++){
vis[i*pri[j]]=;
if(i%pri[j]==)break;
}
}
return;
}
int main(){
n=;
Pri();
int i,j;
f[][]=;
for(i=;i<=cnt;i++){
for(j=n;j>=pri[i];j--){
for(int k=;k<=;k++)
f[j][k]+=f[j-pri[i]][k-];
}
}
while(scanf("%d%d",&n,&k) && n){
printf("%d\n",f[n][k]);
}
return ;
}

POJ3132 Sum of Different Primes的更多相关文章

  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. sicily 1259. Sum of Consecutive Primes

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

  3. [UVa1213]Sum of Different Primes(递推,01背包)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  4. UVa 1213 (01背包变形) Sum of Different Primes

    题意: 选择K个质数使它们的和为N,求总的方案数. 分析: 虽然知道推出来了转移方程, 但还是没把代码敲出来,可能基本功还是不够吧. d(i, j)表示i个素数的和为j的方案数,则 d(i, j) = ...

  5. zoj 2822 Sum of Different Primes (01背包)

    ///给你n 求他能分解成多少个的不同的k个素数相加之和 ///01背包,素数打表 # include <stdio.h> # include <algorithm> # in ...

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

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

  7. UVA 1213 Sum of Different Primes

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

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

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

  9. UVa 1213 Sum of Different Primes (DP)

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

随机推荐

  1. 新建maven的pom.xml第一行出错的解决思路

    前言:博主在想要用maven创建项目的时候,忘记之前已经安装过maven了,所以再安装了另一个版本的maven,导致在pom.xml的第一行总是显示某一个jar的zip文件读取不出来. 在网上找了很多 ...

  2. Gym 100342E Minima (暴力,单调队列)

    3e7暴力,800ms+过,单调队列维护区间最小值. #include<bits/stdc++.h> using namespace std; typedef long long ll; ...

  3. Android(java)学习笔记149:利用开源SmartImageView优化网易新闻RSS客户端

    1.我们自己编写的SmartImageView会有很多漏洞,但是我们幸运的可以在网上利用开源项目的,开源项目中有很多成熟的代码,比如SmartImageView都编写的很成熟的 国内我们经常用到htt ...

  4. CDN加速静态文件服务器的访问

    1.用于加速用户下载资源的速度. 简单来说,CDN相当于一个中间代理,原来我们需要请求某个网址比如www.baidu.com,请求会直接发送至百度的服务器上,假如请求者在新疆,但百度的服务器在北京,这 ...

  5. ucosii(2.89)semaphore 应用要点

    semaphore 的作用:1,允许一个任务与其他任务(中断)同步.2,取得共享资源使用权.3,标志事件的发生.

  6. python小括号( )与中括号 [ ]

    在python中小括号()表示的是tuple元组数据类型,元组是一种不可变序列. >>> a = (1,2,3) >>> a (1, 2, 3) >>& ...

  7. JavaScript中的显示原型和隐形原型(理解原型链)

    显式原型:prototype 隐式原型:__proto__ 1.显式原型和隐式原型是什么? 在js中万物皆对象,方法(Function)是对象,方法的原型(Function.prototype)是对象 ...

  8. (53)zabbix模板

    zabbix模板是做什么的? 平时工作中,我们需要监控web.mysql.redis.nginx这些服务器,众多服务器的业务都是一样的,所以我们只要事先创建好模板,然后所有服务器链接这个模板即可,如果 ...

  9. json数据格式 与 for in

    格式一: var json1={ name:'json', age:'23' }; json1.name='金毛'; 格式二: (比较安全)  属性名字里有空格或者有连字符‘-’或者有保留字例如‘fo ...

  10. verilog RTL 编程实践之五

    How to build and test a module 1.test have: generate .stimulus .check .respose 2.only one monitor ca ...