#t397. 质数和分解(prime)
质数和分解(prime)
Prime Sum Decomposition
Problem Background
Special for beginners, ^_^
Problem Description
Any natural number can be written as a sum of primes each between and (including the case of a single number), and there may be more than one such prime-sum expression. For example, has four essentially different prime-sum expressions: $$9 = 2+5+2 = 2+3+2+2 = 3+3+3 = 2+7$$
Two expressions are considered essentially the same if one can be obtained from the other simply by reordering the terms.
Write a program to compute how many essentially different prime-sum expressions a natural number can be written as.
Input Format
Multiple test cases. Each line contains one natural number , .
Output Format
For each natural number , output the number of essentially different prime-sum expressions, one per line.
Sample Input 1
2
Sample Output 1
1
Sample Input 2
200
Sample Output 2
9845164
Constraints
.
Time limit 1s, memory limit 1024KiB for each test case.
Hint
Approach 1:
Step 1 – Divide into stages by the primes used.
Step 2 – Define state: f[i][j] = number of ways to make sum using the first primes.
Step 3 – Transition: $f[i][j]=\sum_{k=0}^{\lfloor j/p[i]\rfloor}f[i-1][j-k\ast p[i]]$, where is the -th prime.
Step 4 – Initial value: f[0][0]=1.
Approach 2:
Transition: f[i][j] = f[i-1][j] + f[i][j-p[i]]; // for