Sum of Different Primes
来源:互联网 发布:中国软件行业 编辑:程序博客网 时间:2024/04/29 13:38
Description
A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integersn andk, you should count the number of ways to expressn as a sum ofk 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. Forn = 24 andk = 2, the answer is three, because there are three sets {5, 19}, {7, 17} and {11, 13}. Forn = 2 andk = 1, the answer is one, because there is only one set {2} whose sum is 2. Forn = 1 andk = 1, the answer is zero. As 1 is not a prime, you shouldn’t count {1}. Forn = 4 andk = 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 givenn andk.
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 integersn andk separated by a space. You may assume thatn ≤ 1120 andk ≤ 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 forn andk 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
#include<stdio.h>#include<string.h>/*内存使用比较大,状态设成3维的数组,dp[i][j][k],取到第i个质数用了k个数凑成j的方案数*/int dp[200][1121][15],primes[200],isPrime[1121];int main(){ int i,j,k,n,cal; memset(isPrime,1,sizeof(isPrime)); cal=0; /*求出所有需要的素数*/ for(i=2;i<1121;i++){ if(isPrime[i]){ primes[cal++]=i; for(j=2;j*i<1121;j++) isPrime[i*j]=0; } } /*dp----01背包*/ dp[0][0][0]=1; for(i=1;i<=cal;i++){ for(j=0;j<1121;j++){ for(k=0;k<15;k++){ dp[i][j][k]=dp[i-1][j][k]; if(primes[i-1]<=j&&k) dp[i][j][k]+=dp[i-1][j-primes[i-1]][k-1]; } } } while(scanf("%d %d",&n,&k)&&(n||k)) printf("%d\n",dp[cal][n][k]); return 0;}
这是个01背包的问题,设计的状态是取到第I个质数用了k个数凑成j的方案数,上面的代码是三维数组,内存12560k,时间0ms下面进行对内存的优化...
下面的代码内存236KB,时间47ms...#include<stdio.h>#include<string.h>/*内存使用比较小,状态设成2维的数组*/int dp[1121][15],primes[200],isPrime[1121];int main(){ int i,j,k,n,cal; memset(isPrime,1,sizeof(isPrime)); cal=0; for(i=2;i<1121;i++){ if(isPrime[i]){ primes[cal++]=i; for(j=2;j*i<1121;j++) isPrime[i*j]=0; } } dp[0][0]=1; for(i=1;i<=cal;i++){ for(j=1120;j>=0;j--){ for(k=14;k>=0;k--){ if(primes[i-1]<=j&&k) dp[j][k]+=dp[j-primes[i-1]][k-1]; } } } while(scanf("%d %d",&n,&k)&&(n||k)) printf("%d\n",dp[n][k]); return 0;}
- Sum of Different Primes
- UVa1213 Sum of Different Primes
- poj3132 Sum of Different Primes
- poj3132 Sum of Different Primes
- Sum of Different Primes UVA
- WOJ1274-Sum of Different Primes
- Sum of Different Primes UVA
- POJ 3132 Sum of Different Primes
- Poj 3132 Sum of Different Primes
- POJ3122 - Sum of Different Primes - 动态规划
- DP Sum of Different Primes POJ 3132
- zoj 2822 - Sum of Different Primes
- POJ3132——Sum of Different Primes
- 5 POJ 3132 Sum of Different Primes
- 1213 - Sum of Different Primes(DP)
- UVA 1213 Sum of Different Primes
- UVA 1213 Sum of Different Primes
- UVA 1213(p343)----Sum of Different Primes
- 渗透入门
- 防xss攻击,需要对请求参数进行escape吗?
- 正则表达式的组成规则
- MySQL系列(1)--面试常用的
- 1010. Radix
- Sum of Different Primes
- svn提交中文日志
- 正则表达式的构造摘要
- 关于ANR的官方建议
- Git回滚远程版本
- js第十节-日期对象,网页时钟,倒计时
- 在Mac下使用OpenCV, 在Xcode下使用OpenCV
- Android仿微信图片上传,可以选择多张图片,缩放预览,拍照上传等 .
- java正则表达式简介