5: Decompose
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
3s | 8192K | 318 | 182 | Standard |
Give you an positive integer N(1<=N<=30), you can decompose n to several positive integers: A1, A2, ... Ak. (1<=k<=N) And A1 + A2 + ... + Ak = N. Now i want to know what's the maximal product of these k integers.
Input
The input contains several test cases. For each test case it contains a positive integer N. The end of input is indicated by end-of-file.Ouput
For each test case, output K, here K is the maximal product of decomposed numbers.Sample Input
3 5 6
Sample Output
3 6 9
Problem Source: sharang
This problem is used for contest: 43
此类题目,要拆分成小质数。一般情况下就是拆分成多少个3或是多少个2。这是基本的思路。
假设拆分成的数字里面不只是质数,如里面有6,那么6可以拆成两个3,很显然,乘以6要小于乘以3乘3。
先从小的数字开始进行分析:4拆分成2+2,2×2=4,这是最大积,4也可以不拆分。5,拆分成3+2得到的乘积是最大的,6拆分成3+3得到的乘积是最大的。分析到这时就可以了,结论就是拆分成3的和,当最后余数是1的时候,那么就拆成最后一个数是4,或者是最后是两个2。当余数是2的时候,就不用特殊变动。
本题:25÷3=8余1,所以拆分成7个3和1个4(2个2)。
#include<stdio.h> #include<math.h> int n; double ans; int main(){ while(scanf("%d",&n)!=EOF){ ans=1; if(n==1){ printf("1\n"); continue; } if(n%3==1){ ans=pow(3,(n-4)/3); ans*=4; }else if(n%3==2){ ans=pow(3,n/3); ans*=2; }else ans=pow(3,n/3); printf("%.0lf\n",ans); } return 0; }