乘积最大的分解(数学)

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1293

思路:设n = x1+x2+x3.....,要使f[x] = x1*x2*x3....最大,则x1=x2=x3.... 即f[x] = x^(n/x) , 两边同时取对数得:ln(f[x]) = (n/x)lnx, 令g(x) = ln(f[x]) =  (n/x)lnx,对g(x)求导 得:g(x)' = n*(1-lnx), 令 g(x)' = 0 得  x = e = 2.718..... 因为x为正整数,所以x=2或x=3时取得最大值。又3个2的和等于2个3的和,但是3个2的积小于2个3的积,故应将n分为尽可能

多的3的和。将n分为若干个3的和有以下几种情况:

若 n <= 3, f[x] = n. 否则

(1) 如果n%3=0,则 f[x] = pow(3,n/3)

(2) 如果n%3=1,则 f[x] = pow(3,n/3-1)*4

(3) 如果n%3=1,则  f[x] = pow(3,n/3)*2

 1 #include <stdio.h>
 2 #include <math.h>
 3 int main()
 4 {
 5     int n;
 6     while(~scanf("%d",&n)&&n)
 7     {
 8         int m = n%3;
 9         int t = n/3;
10         long long ans = 0;
11         if (m==0)
12             ans = pow(3,t);
13         else if (m==1)
14             ans = pow(3,t-1)*4;
15         else
16             ans = pow(3,t)*2;
17 
18         printf("%lld
",ans);
19     }
20     return 0;
21 }
View Code
原文地址:https://www.cnblogs.com/lahblogs/p/3598833.html