The Ultimate edition of the number division--Knapsack method to solve the division of various numbers

Source: Internet
Author: User

Example: NOI 7219: Complex integer Partitioning problem
Total time limit:
200ms
Memory Limit:
65536kB
Describe

The positive integer n is represented as a series of positive integers of sum, n=n1+n2+...+nk, wherein N1>=n2>=...>=nk>=1, k>=1.
This representation of a positive integer n is called a division of positive integer n.

Input
The standard input contains several sets of test data. Each set of test data is a single row of input data, including two integers n and K.
(0 < n <=, 0 < K <= N)
Output
For each set of test data, output the following three rows of data:
First line: Number of divisions of n divided into k positive integers
Second line: Number of divisions of n divided into a number of different positive integers
Line three: number of divisions of n divided into several odd positive integers
Sample input
5 2
Sample output
233
Tips
First line: 4+1, 3+2,
Second line: 5,4+1,3+2
Line three: 5,1+1+3, 1+1+1+1+1+1
#include <cstdio>#include<cstring>#include<iostream>using namespacestd;Const intn=Wuyi;intF1[n][n],f2[n],f3[n];intSum[n],jsum[n];intn,k;voidinit () {f2[0]=f3[0]=1;  for(intI=1; i<n;++i) sum[i]=i; intt=0;  for(intI=1; i<n;i+=2) jsum[++t]=i;  for(intI=1; i<n;++i) for(intj=1; j<=i;++j) {if(j==1) F1[i][j]=1; Else if(i>=j) F1[i][j]=f1[i-1][j-1]+F1[I-J][J];/*number of divisions of n divided into k positive integers*/            }     for(intI=1; i<n;++i) for(intj=n-1; j>=sum[i];--j) F2[j]+=f2[j-sum[i]];/*number of divisions of n divided into several different positive integers*/     for(intI=1; i<=t;++i)/*Note that this is T, not N.*/       for(intj=jsum[i];j<n;++j) F3[j]+=f3[j-jsum[i]];/*number of divisions of n divided into several odd positive integers*/}intMain () {init ();  while(SCANF ("%d%d", &n,&k) = =2)    {        if(n==0) printf ("0\n0\n0\n"); Elseprintf ("%d\n%d\n%d\n", F1[n][k],f2[n],f3[n]); }    return 0;}
View Code

First line: Number of divisions of n divided into k positive integers
Second line: Number of divisions of n divided into a number of different positive integers
Line three: number of divisions of n divided into several odd positive integers

Analysis: For the "N divided into K-positive integer sum of the number of divisions", can not use knapsack method, or the traditional f[n][m]=f[n-1][m-1]+f[n-m][m];

And for n divided into several different positive integers of the number of division, you can generate a positive integer sequence as an item, run 01 backpack, the title is exactly put up the number of backpacks, note f[0]=1 on it.

For the number of divisions of "N divided into a number of odd positive integers", a positive odd number sequence is generated as an item, and a full backpack or 01 backpack depends on whether there are "different numbers".

For "dividing n into a maximum number not exceeding the number of K." ", create a 1--k sequence, run 01 backpacks or a full backpack.

The Ultimate edition of the number division--Knapsack method to solve the division of various numbers

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.