Algorithm note Integer Division

Source: Internet
Author: User

For example, if 6 is divided into the following 11 types, p (6) = 116; 5 + 1; 4 + 2, 4 + 1 + 1; 3 + 3, 3 + 2 + 1, 3 + 1 + 1 + 1; 2 + 2 + 2, 2 + 2 + 1 + 1, 2 + 1 + 1 + 1 + 1; 1 + 1 + 1 + 1 + 1 + 1; in all the partitions of positive integer n, the number of partitions with the maximum addition n1 not greater than m is recorded as q (n, m ). we can establish the following recursive relationship: (1) q (n, 1) = 1, n> = 1 The maximum addition number is not greater than 1, then there is only one division, n = 1 + 1 + 1 +... + 1 (2) q (n, m) = q (n, n), m> = n maximum addition n1 cannot be greater than n. therefore, q (1, m) = 1 (3) q (n, n) = 1 + q (n, n-1) the division of positive integer n includes the division of n1 = n and the division of n1 <n-1 (4) q (n, m) = q (m-1) + q (n-m, m), n> m> 1 the division of the maximum addition number n1 of positive integer n is not greater than m is composed of the division of n1 = m and the division of n1 <= M-1. calculate the recursion of q (n, m ). The function is as follows. obviously p (n) = q (n, n) int q (int n, int m) {if (n <1) | (m <1) return 0; if (n = 1) | (m = 1) return 1; if (n <m) returnq (n, n); if (n = m) return (q (m-1) + 1); if (n> m) return (q (n m-1) + q (n-m, m ));} in order to find all the divisions of n, we need to save the Division elements and output them in the process of finding the number of divisions of n. [cpp] # include <iostream> using namespace std; int q (int n, int m) {if (n <1 | m <1) return 0; if (n = 1 | m = 1) return 1; if (n <m) return q (n, n); if (n = m) return q (m-1) + 1; return q (m-1) + q (n-m, m); // return 0;} int main () {// cout <"!!! Hello World !!! "<Endl; // prints !!! Hello World !!! Cout <q (10, 10); return 0 ;}

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.