您的位置:首页 > 其它

FZU 1698 最大乘积

2014-10-20 22:49 183 查看
链接:http://acm.fzu.edu.cn/problem.php?pid=1698


 Problem 1698 最大乘积

Accept: 244    Submit: 1020

Time Limit: 1000 mSec    Memory Limit : 32768 KB



 Problem Description

一个正整数一般可以分为几个互不相同的自然数的和,如3=1+2,4=1+3,5=1+4=2+3,6=1+5=2+4,…。现在你的任务是将指定的正整数n分解成若干个互不相同的自然数的和,且使这些自然数的乘积最大。



 Input

只一个正整数n,(3<= n< 10000)。



 Output

第一行是分解方案,相邻的数之间用一个空格分开,并且按由小到大的顺序。 第二行是最大的乘积。



 Sample Input

10



 Sample Output

2 3 5

30



 Source

FOJ-2009年4月月赛 Coral

思路: 题目意思很明显,贪心,一个数分解的越多越好,因为a*b>=a+b(a>1&&b>1),贪心从分解2,直到无法再继续分解为止,剩下的值分别逆序一一累加到已分解的值上,最后因为最多可以分解到140多,所以需要用高精度乘法,嫌麻烦用java大数水过了,,
注意: 因为至少要分解成两个数,所以n=3和n=4时要特判一下,,最坑人的是java的换行符,在windows系统是"\r\n",在unix系统下才是"\n",PE根本停不下来,这让C++党情何以堪,,
import java.util.*;
import java.math.BigInteger;

public class Main{
public static void main(String[]args){
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
int n=scan.nextInt();
if(n==3){
System.out.print(1+" "+2+"\r\n"+2+"\r\n");
continue;
}
if(n==4){
System.out.print(1+" "+3+"\r\n"+3+"\r\n");
continue;
}
int ans[] = new int[200];
int tot=0,tmp=2;
while(n-tmp>=0){
ans[tot++]=tmp;
n-=tmp;
tmp++;
}
while(n>0){
for(int i=tot-1;i>=0;--i){
ans[i]++;
n--;
if(n==0) break;
}
}
BigInteger mul = new BigInteger("1");
for(int i=0;i<tot;++i){
mul=mul.multiply(BigInteger.valueOf(ans[i]));
}
for(int i=0;i<tot-1;++i){
System.out.print(ans[i]+" ");
}
System.out.print(ans[tot-1]+"\r\n"+mul+"\r\n");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: