思路:若干互不相同自然数,通过规律可知:
当N > 5时,将N分解为尽可能多的非1数,所得的乘积最大。e.g. 5 -> 2, 3
6 -> 2, 4
故从2开始累加 2 + 3 + 4 + ..., 但在这种情况下,会出现累加和大于目标数的情况。此时若改变数列后方使之变小则势必与数列前方数值重复,因此在这种情况下直接将多出的(累加和 - 目标)对应数列项删除即可。
e.g. 11 -> 2,3,4,5 exceed 删除 3, 变为2,4,5
也可以理解为2,3,4 余2,可以凭直觉发现,余数加给数列中较小数所得的结果最优,但为避免重复,只能从后向前加。先给4 > 5 再给3 > 4 得到2,4,5。
Code:
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Next {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n == 3) {
System.out.printf("1 2%n2");
return;
}
if (n == 4) {
System.out.printf("1 3%n3");
return;
}
int sum = 0;
int[] cpn = new int[n]; //components
int tmp = 0;
for (int i = 2; sum < n; i++) {
sum += i;
cpn[i] = 1;
tmp = i;
}
if (sum - n > 1) {
cpn[sum - n] = 0;
}else {
cpn[tmp] = 0;
cpn[tmp + 1] = 1;
}
BigInteger muti = BigInteger.valueOf(1);
for (int i = 2; i < n; i++) {
if (cpn[i] == 1) {
System.out.print(i + " ");
muti = muti.multiply(BigInteger.valueOf(i));
}
}
System.out.printf("%n%d", muti);
sc.close();
}
}