poj3132 Sum of Different Primes

Sum of Different Primes

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3293   Accepted: 2052

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 kdifferent 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

先打表找出所有的质数,再转化为与背包问题类似的问题,装态转移方法很简单,dp[i][j] += dp[i-pri][j-1];

dp[i][j]表示i分解成j的个数,pri表示枚举质数,即可解!
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "math.h"
#include <algorithm>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
#define N 1125
#define SCANF scanf_s
bool pri[N];
int prime[200];
int dp[N][15];
int prinum = 0;
int getPrimeNumMid(int sum)
{
	int left = 1, right = prinum - 1, mid;
	while (left < right - 1)
	{
		mid = (left + right) / 2;
		if (prime[mid] > sum)
		{
			right = mid;
		}
		else if (prime[mid] < sum)
		{
			left = mid;
		}
		else
		{
			return mid;
		}
	}
	if (prime[right] <= sum)
		return right;
	else
		return left;
}
void init(int n,int knum)
{
	memset(pri, true, sizeof(pri));
	pri[0] = pri[1] = false;
	for (int i = 2; i < sqrt((float)N); i++)
	{
		for (int j = i + i; j < N; j += i)
		{
			pri[j] = false;
		}
	}
	prinum = 1;
	for (int i = 0; i < N; i++)
	{
		if (pri[i])
		{
			prime[prinum] = i;
			prinum++;
		}
	}
	int pnum = getPrimeNumMid(n);
	//printf("%d\n", prime[pnum]);
	for (int i = 0; i <= n; i++)
	{
		for (int j = 0; j <= knum; j++)
		{
			dp[i][j] = 0;
		}
	}
	dp[0][0] = 1;
	for (int k = 1; k <= pnum; k++)
	{
		for (int i = n; i >= 0; i--)
		{
			for (int j = 1; j <= knum; j++)
			{

				if (i - prime[k] >= 0)
					dp[i][j] += dp[i - prime[k]][j - 1];
				//printf("%d %d %d \n", i, j, dp[i][j]);
			}
		}
	}
}

int main()
{
	int n, knum;
	init(1120,14);
	while (SCANF("%d%d", &n , &knum) != EOF)
	{
		if (n == 0 && knum == 0)
			break;
		printf("%d\n", dp[n][knum]);
	}
	return 0;
}

时间: 2024-10-13 06:37:36

poj3132 Sum of Different Primes的相关文章

POJ 3132 Sum of Different Primes DP背包

http://poj.org/problem?id=3132 题意: 给定n和k,问用恰好k个不同的质数来表示n的方案数. 分析: n和k都很小.反正就是个背包,选k个物品恰好填满n即可. 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 5 bool dp[1200][15]; 6 int ct[1200][15]; 7 int p[1200]; 8 bool a[1200]; 9 int n, k,

POJ 3132 &amp; ZOJ 2822 Sum of Different Primes(dp)

题目链接: POJ:http://poj.org/problem?id=3132 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2822 Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive int

POJ 3132 Sum of Different Primes ( 满背包问题)

Sum of Different Primes Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3280   Accepted: 2040 Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integer

UVA1213 Sum of Different Primes(素数打表+dp)

UVA - 1213 Sum of Different Primes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two

sicily 1259. Sum of Consecutive Primes

Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53

&lt;OJ_Sicily&gt;Sum of Consecutive Primes

Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53

UVA 1213 Sum of Different Primes(经典dp)

题意:选择k(k<15)个唯一质数,求出和为n(n<1121)的可能数 题解:预处理dp,dp[k][n]表示使用k个素数拼成n的总方案数 就是三重枚举,枚举k,枚举n,枚举小于n的素数 但是注意三重循环的顺序与位置,我们要防重防漏 第一重循环是枚举每个小于n的素数,思路是对于每个素数放入dp里面的位置 第二重倒叙枚举每个数n,倒序是类似01背包不能让枚举的素数重复加入同一个dp数组中 第三重正序枚举个数k只能放在最里面,这样才不会出现重复 import java.util.Scanner;

5 POJ 3132 Sum of Different Primes

dp[j][k]表示用j个质数表示k这个值得方法数. 一个质数只能用一次,类似01背包. #include<cstdio> #include<cstring> const int maxn=1130; int pri[maxn+10],dp[20][maxn+10]; void init() { int i,j; for(i=0;i<=maxn;i++) pri[i]=1; pri[0]=pri[1]=0; for(i=2;i<=maxn;i++) { if(!pri[

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

题意: 选择K个质数使它们的和为N,求总的方案数. 分析: 虽然知道推出来了转移方程, 但还是没把代码敲出来,可能基本功还是不够吧. d(i, j)表示i个素数的和为j的方案数,则 d(i, j) = sigma d(i-1, j-p[k]) ,其中p[k]表示第k个素数 注意递推的顺序是倒着推的,否则会计算重复的情况. 代码中第二重和第三重循环的顺序可互换. 1 #include <cstdio> 2 #include <cmath> 3 4 const int maxp = 1