Integer partitioning problem

Source: Internet
Author: User

Describe

Given positive integers n and M, please divide N into several integers A1, A2, ..., Ak (k >= 1), so:

1.0 < A1 < A2 < ... < Ak;

2. A1 + A2 + ... + Ak = N;

3. A1, A2, ..., Ak is different with each other;

4. The product of them P = A1 * A2 * ... * Ak is a multiple of M;

How many different ways can I achieve this goal?

Input

integers n and M. 1 <= n <=, 1 <= M <= 50.

Output

Output One integer-the number of different ways to achieve this goal, module 1,000,000,007.

Sample Hints

There is 4 different ways to achieve this goal for the sample:

A1=1, a2=2, a3=4;

A1=1, a2=6;

a1=2, a2=5;

A1=3, a2=4.

Sample input
9 |
Sample output
4
#include <iostream>using namespace Std;int n=0;//function for printing (output)//result an array that stores the result of a partition, length of this division is 1 (starting from 0) void Display (int *result,int length) {for (int i=0;i<length;i++) cout<<result[i]<< ""; Cout<<endl;} BOOL Norepeat (int *result,int length) {int a[1000]={0};for (int i=0;i<length;i++) {if (a[result[i]]!=0) {return false;} else{a[result[i]]++;}} return true;} BOOL Ismultipleright (int *result,int length,int m) {int a=1;for (int i=0;i<length;i++) {a*=result[i];} if (a%m = = 0) {return true;} return false;} The main partition function q (int n,int m,int *result,int length)//n is the integer to be divided, M is the maximum addend upper limit, result and length mean to be in the same display function//The q is divided into five case class discussion, which contains recursive call int Q (int n,int m,int *result,int length,int multiple) {//when n>=1 and M=1, Q (n,m,result,length) =q (n-1,m,result,length) if (n >=0&&m==1) {//until n=0 and m=1, output I F (n==0) {display (result,length), if (Norepeat (result,length) &&ismultipleright (result,length,multiple)) {N+ +;}} Else{resUlt[length]=1;q (n-1,m,result,length+1,multiple);} return 1;} When N=1 and m>1, the decomposition is complete, output else if (n==1&&m>1) {Result[length]=n;display (Result , length+1); if (Norepeat (result,length) &&ismultipleright (result,length,multiple)) {N++;} return 1;} When N<m, Q (n,m,result,length) =q (n,n,result,length) Else if (n<m) {return Q (n,n, Result,length,multiple);} When N=m, Q (n,m,result,length) =q (n,m-1,result,length) +1 (number of divisions) else if (n==m) {result[l Ength]=m;display (result,length+1); if (Norepeat (result,length) &&ismultipleright (result,length,multiple)) {n++;} return Q (n,m-1,result,length,multiple) +1;}                                                         When N>m>1,//q (n,m,result,length) =q (n-m,m,result,length+1) +q (n,m-1,result,length) Else    {result[length]=m; return Q (n-m,m,result,length+1,multiple) +q (n,m-1,result,length,multiple);}}                      int main () {int n,m;        int result[100]={0},length=0;    cout<< "Please input the integer:";  cin>>n>>m;  cout<< the number of "integers" <<n<< "<<q" (n,n,result,length,m) <<endl;   cout<< "The number of qualified division is" <<N<<endl;  return 0; }


Integer partitioning problem

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.