Question: Prime Summation

Source: Internet
Author: User

Address: click here

The question I of this year's National Defense University competition is: for positive integers N, K, N can be expressed as the sum of several prime numbers, ask the number of valid schemes (the same order of prime numbers is regarded as the same scheme), and output the K-digit representation scheme in the Lexicographic Order.

My first response was to use DFS, and I had a clear idea of coding. At first, I thought it was wrong to save all the statuses. If there are too many solutions that will overflow, I will not save them. Every time I update them, they will not stop until they are updated to the solution. At that time, we did not properly estimate the total number of solutions and kept WA. After we came back and changed it, we passed all the test data, but we had a proper TLE.

This is actually a DP problem. dp [I] [j] is the number of solutions for the first prime number j in the decomposition of positive integer I, the recursive formula is dp [I] [j] = sum (dp [I-j] [k], 2 = <k <= min (I-j, j )), (Because 2 is the first prime number), the complexity of preprocessing starts from 0 to 200, approximate O (N ^ 3), then input a N and K, sum (dp [N] [k], 2 = <k <= N)

The key is to find the next one through the given K inverse, because the DP value of each item is known, and the larger one is placed in the front, so SUM is calculated from the following forward, until SUM> K, we will find the first item, record this item, and use recursion, the next n is n-I, and the next k is k-sum + dp [n] [I]. It should be noted that the front boundary is 2 from the back to the front, however, the following boundary is the one recorded above, because it must be greater than or equal to the following item before decomposition. Then it is found until K = 0. In fact, N = 0 and K = 0 are achieved at the same time. That's it.

AC code

# Include <cstdio> # include <ctype. h >#include <algorithm> # include <iostream> # include <cstring> # include <vector> using namespace std; int dp [201] [201], mark [201], path [105]; // do not define global variables in other functions. Otherwise, the global variables will not change. Do not laugh at me .. This error is returned, so the int nct; void pprime () {int I, j; dp [2] [2] = 1; for (I = 2; I <15; I ++) for (j = 2; I * j <201; j ++) mark [j * I] = 1; for (I = 2; I <201; I ++) if (mark [I] = 0) dp [I] [I] = 1;} int min (int a, int B) {return a> B? B: a;} void init () {int I, j, k, sum; for (I = 2; I <= 200; I ++) {for (j = 2; j <= I; j ++) {if (mark [j]! = 1 & I! = J) {sum = 0; for (k = 2; k <= min (j, I-j); k ++) sum + = dp [I-j] [k]; dp [I] [j] = sum ;}}} void find_path (int n, int k, int w) {int I; if (n = 0 | k = 0) return; int sum = 0; for (I = w; I> = 2; I --) {if (dp [n] [I]! = 0) sum + = dp [n] [I]; if (sum> = k) {path [nct ++] = I; break ;}} find_path (n-I, k-sum + dp [n] [I], I);} int main () {// freopen ("input.txt", "r ", stdin); // freopen ("out. in "," w ", stdout); pprime (); init (); int n, k, I; while (~ Scanf ("% d", & n, & k) {nct = 0; int ans = 0; for (I = 0; I <= 200; I ++) ans + = dp [n] [I]; if (k> ans) k = ans; printf ("% d \ n % d =", ans, n); find_path (n, k, 200); for (I = 0; I <nct; I ++) {printf ("% d", path [I]); if (I! = Nct-1) printf ("+");} printf ("\ n");} return 0 ;} /*************************************** * *********************** Problem: 1425 User: HNU_TEAM_3 Language: C ++ Result: Accepted Time: 0 MS Memory: 1644 kb ************************************** **************************/




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.