11091 optimal natural number decomposition problem
Time limit: 1000MS memory limit: 65535K
Number of submissions: 0 Number of Passes: 0
Question types: programming language: C + +; C VC; Java
Description
Problem Description: Set n is a positive integer. (1) n is now decomposed into the sum of several distinct natural numbers, and the product of these natural numbers is the largest. (2) n is now decomposed into the sum of a number of natural numbers, and the product of these natural numbers is the largest. Programming tasks: For a given positive integer n, the maximum product of the optimal decomposition of a programming calculation problem (1) and (2). Note: 1. The natural number here does not contain 0 but is allowed to be 1. 2. In particular, when the integer n cannot be decomposed into a number of distinct addend, that itself is considered a separate addend, such as input 2, the solution output of the problem (1) is 2. And if the integer n can be decomposed into a number of different addend, regardless of the case itself as a separate addend, such as 4, the solution output of the problem (1) is 3, not 4. 3. A number of distinct natural or several natural numbers, which can be >=1, that is, 1.
Input format
There is only one positive integer n (1<=n<=100).
Output format
Output to solve the problem (1) and (2) the maximum product, the middle space is connected, these two numbers may be larger please use 64-bit integers. For example, input n is 10, if Addend is different, then n=2+3+5, the maximum product is 2*3*5=30. If the addend can be the same, then n=2+2+3+3, at this time the maximum product is 2*2*3*3=36.
Input sample
10
Output sample
30 36
Tips
Analysis: note whether (1) or (2), the product is represented by a 64-bit integer. (1) Decomposition into different natural numbers and note: If A+b equals a constant, the smaller the |a-b|, the greater the AB. To make addend different, as much as possible concentration, that addend can only be a continuous natural number. Greedy strategy: divides n into the number of consecutive natural numbers starting from 2. If the last number is left, the remaining number is evenly distributed to the preceding items in the latter priority mode. (2) Decomposition into a number of natural numbers and note that: if A+b equals a constant, the smaller the |a-b|, the greater the AB. If n = m1+m2+...+mk, then-1 <= (MI-MJ) <= 1, (1<=i<=k, 1<=j<=k), that is, any addend gap does not exceed plus or minus 1. Since the splitting of the addend can be the same, any one number after the product is better than the total, so split to the extreme, the extreme addend is 3 or 2, and the split 3 is better than the split 2, so the priority is split to 3. Greedy strategy: The very best disassembly, as far as possible first to split N into 3,3,3,..., 3, if split into a number of 3 after remaining, then 2, or 2 and 2. The induction formula is as follows: 1) Max{m1*m2*...*mk} = 3^ (N/3) if n (mod 3) is equal to the 4*3^[) max{m1*m2*...*mk} = (n-4)/3] if n (mod 3) = max{m1*m2*. .. *MK} = 2*3^[(n-2)/3] if n (mod 3) equals 2 How to use the 64-bit integer involved in this question? The compilation environment is different, the definition of 64-bit integers and the input and output are slightly different: 1) The GNU gcc/g++ a long long type, or unsigned long long, the input output is directly output with CIN and cout, It is also possible to use scanf and printf (but this OJ system does not support!) )。 Long Long a;cin >> a;cout << A; You can also use: (Note that the gcc/g++ of this OJ system does not support a 64-bit integer output in "%i64d" form, but the standard GNU GCC is supported as follows, can be run without error on codeblocks) long Long a;scanf ("%i64d", &a);p rintf ("%i64d", a); 2) VC with __int64 type, or unsigned __int64__int64 A; scanf ("%i64d", &a);p rintf ("%i64d", a); VC, 64 integers do not use CIN and cout to enter the output, it is saidVC under 64-bit integer compatibility is not good, will be wrong! You can test the following program under the VC will be wrong? __int64 a;cin >> a;cout << A;
Author
Zhengchan
My implementation code:
#include <iostream> #include <math.h>using namespace std;/* test data: 1 2 3 4 */int main () {int n; CIN >> N; if (n = = 1 | | n = = 2) {//exclude n=1,2 case cout << n << "" << N; }else{//non-identical natural number and long long _res1 = 1; if (n = = 3 | | n = = 4) {//exclude n=3,4 case _res1 = n-1; }else{int _count = 0; int _rest = 0,_avg = 0,_mod = 0; int a[n + 1];//a[0] and a[1] not used for (int i = 2; I <= n; i++) {//Initialize a A[i] = 1; } for (int i = 2; I <= n; i++) {_count + = i; if (_count > N) {_rest = n-(_count-i);//The extra part if (_rest < (i-2)) {//Not enough each selected number is divided into 1 _avg = 1; _mod = 0; }else{//each selected number can be at least 1 _avg = _rest/(i-2);//average number of previous i-2 determined _mod = _r Est% (i-2);//The remainder of the average after} for (int j = i-1; J >= I-_rest; j--) {//from back to front to allocate more parts a[j] + = _avg; } A[i-1] + = _mod;//last +_mod break; } A[i] = i; } for (int i = 2; I <= n; i++) {_res1 *= a[i]; }}//The sum of some natural numbers and/* Inductive formula is as follows: 1) Max{m1*m2*...*mk} = 3^ (N/3) if n (Mo d 3) equals 0 2) Max{m1*m2*...*mk} = 4*3^[(n-4)/3] if n (mod 3) equals 1 3) Max{m1*m2*...*mk} = 2*3^[(n-2)/3] if n (mod 3) equals 2 */long long _res2 = 1; if (n% 3 = = 0) {_res2 = POW (3,N/3); }else if (n% 3 = = 1) {_res2 = 4*pow (3, (n-4)/3); }else{_res2 = 2*pow (3, (n-2)/3); } cout << _res1 << "<< _res2; } cout << Endl; return 0;}
11091 optimal Natural number decomposition problem (greedy)