码迷,mamicode.com
首页 > 其他好文 > 详细

poj3132 Sum of Different Primes

时间:2015-05-19 20:56:40      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

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


poj3132 Sum of Different Primes

标签:

原文地址:http://blog.csdn.net/mengzhengnan/article/details/45849393

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!