Number of distinct ways to represent a number as sum of K unique primes
Given an integer N, and an integer K, the task is to count the number of distinct ways to represent the number N as a sum of K unique primes.
Note: Distinct means, let N = 7 and K = 2, then the only way can be {2,5}, because {5,2} is same as {2,5}. So only 1 way.
Examples:
Input: N = 10, K = 2 Output: 1 Explanation: The only way is {3, 7} or {7, 3} Input: N = 100, K = 5 Output: 55
Approach: The problem can be solved using Dynamic Programming and Sieve of Eratosthenes.
 Let dp[i][j][sum] be our 3D DP array, which stores the number of distinct ways to form a sum using j number of primes where the last index of prime selected is i in the prime vector.
 The prime numbers can be efficiently computed using Sieve of Eratosthenes. So, we can get a check of prime in O(1) time.

Recurrence:
We can either include this current prime to our sum, or we can exclude it.
dp[i][j][sum] = solve(i+1, j+1, sum+prime[i]) + solve(i+1, j, sum)
Below is the implementation of the above approach :
// C++ program to count the Number // of distinct ways to represent // a number as K different primes #include <bits/stdc++.h> using namespace std; // Prime vector vector< int > prime; // Sieve array of prime bool isprime[1000]; // DP array int dp[200][20][1000]; void sieve() { // Initialise all numbers // as prime memset (isprime, true , sizeof (isprime)); // Sieve of Eratosthenes. for ( int i = 2; i * i <= 1000; i++) { if (isprime[i]) { for ( int j = i * i; j <= 1000; j += i) { isprime[j] = false ; } } } // Push all the primes into // prime vector for ( int i = 2; i <= 1000; i++) { if (isprime[i]) { prime.push_back(i); } } } // Function to get the number of // distinct ways to get sum // as K different primes int CountWays( int i, int j, int sum, int n, int k) { // If index went out of prime // array size or the sum became // larger than n return 0 if (i > prime.size()  sum > n) { return 0; } // If sum becomes equal to n and // j becomes exactly equal to k. // Return 1, else if j is still // not equal to k, return 0 if (sum == n) { if (j == k) { return 1; } return 0; } // If sum!=n and still j as // exceeded, return 0 if (j == k) return 0; // If that state is already // calculated, return directly // the ans if (dp[i][j][sum]) return dp[i][j][sum]; int inc = 0, exc = 0; // Include the current prime inc = CountWays(i + 1, j + 1, sum + prime[i], n, k); // Exclude the current prime exc = CountWays(i + 1, j, sum, n, k); // Return by memoizing the ans return dp[i][j][sum] = inc + exc; } // Driver code int main() { // Precompute primes by sieve sieve(); int N = 100, K = 5; cout << CountWays(0, 0, 0, N, K); } 
55
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a studentfriendly price.
Recommended Posts:
 Number of ways to represent a number as sum of k fibonacci numbers
 Maximum number of distinct positive integers that can be used to represent N
 Count of ways to represent N as sum of a prime number and twice of a square
 Number of ways to form a heap with n distinct integers
 Count the number of ways to fill K boxes with N distinct items
 Number of ways to form an array with distinct adjacent elements
 Number of ways of distributing N identical objects in R distinct groups
 Number of ways of distributing N identical objects in R distinct groups with no groups empty
 Minimum number of Binary strings to represent a Number
 Minimum number of given powers of 2 required to represent a number
 Represent a number as a sum of maximum possible number of Prime Numbers
 Print a number strictly less than a given number such that all its digits are distinct.
 Number of ways to split a binary number such that every part is divisible by 2
 Number of ways to calculate a target number using only array elements
 Count number of ways to divide a number in 4 parts
 Find the number of ways to divide number into four parts such that a = c and b = d
 Represent the given number as the sum of two composite numbers
 Different ways to represent N as sum of K nonzero integers
 Find Next number having distinct digits from the given number N
 Count of matchsticks required to represent the given number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.