Optimal decomposition problem

Problem Description: Let n be a positive integer, n is now required to decompose into a number of mutually different natural numbers and the maximum product of these natural number.
Input 10
Output 30
(is decomposed 523)

 Topic analysis:

1, if a + b is a constant, then | ab & | smaller, a * b greater. To make addend different from each other, and focus as much as possible, it is only addend a continuous natural number.
2, a number of breaks down, the product will be greater than before decomposition. The 6, 2 and 4 into a product of 8> 6

#include <iostream>
#define mmax 100000
using namespace std;
long long int div(long long int n){
	if(n<=4)
		return n;
	int p[mmax],i; 
	for(i=0;i<n;i++){		
		p[i]=i+2;
		n=n-(i+2);
		if(n>p[i])
			continue;
		else{
			for(int j=i;j>0;j--){
				if(n<=0)
					break;
				else{
					p[j]=p[j]+1;
					n=n-1;
				}
			}
			break;
		}
	}
	long long int sum=1;
	for(int j=0;j<=i;j++)
	sum=sum*p[j];
	return sum;
}
int main(){
	long long int n;
	cin>>n;
	cout<<div(n)<<endl;
	return 0;
}

 

Published 43 original articles · won praise 23 · views 5321

Guess you like

Origin blog.csdn.net/weixin_43442778/article/details/89677663