UVA1213 Sum of Different Primes (prime number of tables +DP)

Source: Internet
Author: User

UVA-1213Sum of Different Primes
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &%llu

Submit Status

Description

A positive integer may is expressed as a sum of different prime numbers (primes), in one-or another. Given-positive integers n andK, you should count the number of ways to express n as a sum o F k different primes. Here, the ways is considered to being the same if they sum up the same set of the primes. For example, 8 can is expressed as 3 + 5 and 5 + 3 But they is not distinguished.

WhenNandkIs -and3Respectively, the answer is both because there is and sets{2, 3, +}and{2, 5, +}Whose sums is equal to -. There is no other sets of three primes, sum up to -. For N =and k = 2, the answer is three, because there is three sets{5, +},{7, +}and{One, one}. For N = 2and k = 1, the answer is one, because there are only one set{2}Whose sum is2. For N = 1and k = 1, the answer is zero. As1Isn't a prime, you shouldn ' t count{1}. For N = 4and k = 2, the answer is zero, because there was no sets of the different primes whose sums are4.

Your job is to write a program this 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, and zeros separated by a space. A DataSet is a line containing the positive integers n and K separated by a space. Assume that n1120 and k.

Output

The output should is composed of lines, each corresponding to an input dataset. An output line should contain one non-negative integer indicating the number of ways for n and K Specifi Ed in the corresponding dataset. Assume that it's 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

Root:: AOAPC ii:beginning algorithm Contests (Second Edition) (Rujia Liu):: Chapter 10. Maths:: Exercises
Root:: Competitive programming 2:this increases the lower bound of programming contests. Again (Steven & Felix Halim):: Problem Solving Paradigms:: Dynamic Programming:: 0-1 knapsack (subset Sum)
Root:: Competitive programming 3:the New Lower Bound of programming contests (Steven & Felix Halim):: Problem Solvi Ng Paradigms:: Dynamic Programming:: 0-1 knapsack (subset Sum)

Submit Status


Test instructions is the choice of K prime numbers to make it and N, first a prime table and then dp,dp[i][j] for the selection of J number and bit I of the scheme number.



#include <bits/stdc++.h> #define foreach (it,v) for (__typeof ((v). Begin ()) it = (v). Begin (); it! = (v). End (); ++it) Using namespace Std;typedef long long ll;const int maxn = 1120;bool check[maxn];int prime[1120];ll dp[1122][15];int Init (i    NT N) {memset (check,0,sizeof check);    int tot = 0;    Check[0] = check[1] = 1;        for (int i = 2; I <= n; i++) {if (!check[i]) prime[tot++] = i;            for (int j = 0, J < tot; ++j) {if (ll) i*prime[j]>n) break;            CHECK[I*PRIME[J]] = true;        if (i%prime[j]==0) break; }} return tot;}    int main () {int n,k;    int tot = init (maxn-1);       while (~SCANF ("%d%d", &n,&k) &&n) {memset (dp,0,sizeof DP);       Dp[0][0] = 1;            for (int i = 0; prime[i]<=n&&i < tot; i++) {int limt = min (i+1,k);            int w = prime[i]; for (int V = n; V >= W; v--) for (int j = 1; J <= Limt; j + +) if (Dp[v-w][j-1]) {Dp[v][j] + = dp[v-w][J-1];    }} cout<<dp[n][k]<<endl; } return 0;}



UVA1213 Sum of Different Primes (prime number of tables +DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.