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

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

题意:

给出n,k问将n分解成k个素数有多少种分法。

分析:

首先使用素数筛筛选出素数。

设dp[i][j]:将j分解成i个素数的方案数,那么:dp[i][j]=dp[i-1][j-su[k]]。

for枚举所有素数

  for枚举1150->1所有的值

    for枚举方案14->1

最后读入n,k直接输出dp[k][n]即可。

AC code:

#include<cstdio>
#include<cstring>
using namespace std;
bool u[];
int su[];
int dp[][];
int psu[];
int num;
void olas()
{
num=;
memset(u,true,sizeof(u));
for(int i=;i<=;i++)
{
if(u[i]) su[num++]=i;
for(int j=;j<num;j++)
{
if(i*su[j]>) break;
u[i*su[j]]=false;
if(i%su[j]==) break;
}
}
psu[]=su[];
for(int i=;i<num;i++)
{
psu[i]=psu[i-]+su[i];
}
}
void pre()
{
dp[][]=;
for(int i=;i<num;i++)
{
for(int j=;j>=;j--)
{
if(j>=su[i])
{
for(int k=;k>=;k--)
{
dp[k][j]+=dp[k-][j-su[i]];
}
}
else break;
}
}
}
int main()
{
int n,k;
olas();
pre();
freopen("input.txt","r",stdin);
while(~scanf("%d%d",&n,&k)&&n&&k)
{
printf("%d\n",dp[k][n]);
}
return ;
}

POJ 3132 DP+素数筛的更多相关文章

  1. codeforces 822 D. My pretty girl Noora(dp+素数筛)

    题目链接:http://codeforces.com/contest/822/problem/D 题解:做这题首先要推倒一下f(x)假设第各个阶段分成d1,d2,d3...di组取任意一组来说,如果第 ...

  2. Codeforces 264B Good Sequences(DP+素数筛)

    题目链接:http://codeforces.com/problemset/problem/264/B 题目大意:给出n个单调递增的数,让你找出最长的好序列,好序列是一种单调递增的并且相邻元素的最大公 ...

  3. poj 2689 区间素数筛

    The branch of mathematics called number theory is about properties of numbers. One of the areas that ...

  4. codeforces 569C C. Primes or Palindromes?(素数筛+dp)

    题目链接: C. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes in ...

  5. 素数筛 poj 2689

    素数筛 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; ...

  6. poj 3048 Max Factor(素数筛)

    这题就是先写个素数筛,存到prime里,之后遍历就好,取余,看是否等于0,如果等于0就更新,感觉自己说的不明白,引用下别人的话吧: 素数打表,找出20000之前的所有素数,存入prime数组,对于每个 ...

  7. POJ 3126 Prime Path (bfs+欧拉线性素数筛)

    Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...

  8. Prime Path素数筛与BFS动态规划

    埃拉托斯特尼筛法(sieve of Eratosthenes ) 是古希腊数学家埃拉托斯特尼发明的计算素数的方法.对于求解不大于n的所有素数,我们先找出sqrt(n)内的所有素数p1到pk,其中k = ...

  9. Codeforces 385C - Bear and Prime Numbers(素数筛+前缀和+hashing)

    385C - Bear and Prime Numbers 思路:记录数组中1-1e7中每个数出现的次数,然后用素数筛看哪些能被素数整除,并加到记录该素数的数组中,然后1-1e7求一遍前缀和. 代码: ...

随机推荐

  1. Lambda(一)lambda表达式初体验

    Lambda(一)lambda表达式初体验 Lambda引入 : 随着需求的不断改变,代码也需要随之变化 需求一:有一个农场主要从一堆苹果中挑选出绿色的苹果 解决方案:常规做法,source code ...

  2. Ajax异步后台加载Html绑定不上事件

    因项目需要,需要实时从后台动态加载html,开发过程中,遇到事件绑定不上,后来百度一番,大概意思:ajax是异步加载的,页面一开始绑定事件的时候,后台数据还没有传过来,就绑定事件,这个时候找不到这个d ...

  3. react-custom-scrollbars的使用

    react-custom-scrollbars的作用 流畅的本机浏览器滚动 移动设备的本机滚动条 完全可定制 自动隐藏 自动高度 通用(在客户端和服务器上运行) requestAnimationFra ...

  4. 「白帽挖洞技能」YxCMS 1.4.7 漏洞分析

    这几天有小伙伴留言给我们,想看一些关于后台的漏洞分析,今天i春秋选择YxCMS 1.4.7版本,理论内容结合实际案例进行深度分析,帮助大家提升挖洞技能. 注:篇幅较长,阅读用时约7分钟. YXcms是 ...

  5. iOS 快速打包方法

    这种打包方式应该是目前所有打包方式中最快的,就是编译工程--找到.app文件--新建Payload文件夹--拷贝.app到Payload文件夹--压缩成zip--更改后缀名为ipa--完成! 注意事项 ...

  6. 理解Java方法增强

    在实际开发中,我们往往需要对某些方法进行增强,常用的方法增强的方式有三种. 类继承 .方法覆盖 必须控制对象创建,才能使用该方式 装饰者模式方法加强 必须和目标对象实现相同接口或继续相同父类,特殊构造 ...

  7. Mac下多版本pip共存

    Mac下多版本pip共存 来自于官方的解释, pip是python包管理工具, 该工具提供了对python包的查找, 下载, 安装, 卸载等功能python第三方工具包多数依赖于pip进行安装, 如 ...

  8. Linux下用火焰图进行性能分析【转】

    转自:https://blog.csdn.net/gatieme/article/details/78885908 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原 ...

  9. JAVAWEB复习day01

    一.基础知识 1.一个html文件开始和结束的标签<html></html> 2.html的两部分<head>设置相关信息</head>,<bod ...

  10. 商学院教授点评亚马逊、苹果、Facebook和谷歌的商业策略:3星|《互联网四大:亚马逊、苹果、脸书和谷歌的隐藏基因》

    “ 谷歌依靠时报的内容吸引了数十亿点击量,而时报使用它们的搜索算法来引入流量.但是两者中显然谷歌拥有更大的权力.它如同地主一样统治着互联网的一个关键领域,而时报就相当于那块草地上的佃农.我们的结局从一 ...