最强DE 战斗力
时间限制:
1000 ms | 内存限制:
65535 KB
难度:3
-
描述
-
春秋战国时期,赵国地大物博,资源非常丰富,人民安居乐业。但许多国家对它虎视眈眈,准备联合起来对赵国发起一场战争。
显然,面对多个国家的部队去作战,赵国的兵力明显处于劣势。战斗力是决定战争成败的关键因素,一般来说,一支部队的战斗力与部队的兵力成正比。但当把一支部队分成若干个作战队伍时,这个部队的战斗力就会大大的增强。
一支部队的战斗力是可以通过以下两个规则计算出来的:
1.若一支作战队伍的兵力为N,则这支作战队伍的战斗力为N;
2.若将一支部队分为若干个作战队伍,则这支部队的总战斗力为这些作战队伍战斗力的乘积。
比如:一支部队的兵力为5时的战斗力分析如下:
显然,将部队分为2个作战队伍(一个为2,另一个为3),总的战斗力达到最大!情况
作战安排
总的战斗力
1
1,1,1,1,1(共分为5个作战队伍)
1*1*1*1*1=1
2
1,1,1,2 (共分为4个作战队伍)
1*1*1*2=2
3
1,2,2 (共分为3个作战队伍)
1*2*2=4
4
1,1,3 (共分为3个作战队伍)
1*1*3=3
5
2,3 (共分为2个作战队伍)
2*3=6
6
1,4 (共分为2个作战队伍)
1*4=4
7
5 (共分为1个作战队伍)
5=5
-
输入
-
第一行: N表示有N组测试数据. (2<=N<=5)
接下来有N行,每行有一个整数Ti 代表赵国部队的兵力. (1<=Ti<=1000) i=1,…N
输出
- 对于每一行测试数据,输出占一行,仅一个整数S,表示作战安排的最大战斗力. 样例输入
-
254
样例输出
-
64
-
第一行: N表示有N组测试数据. (2<=N<=5)
数学方法:答案和3有关
import java.math.BigInteger;
import java.util.Scanner;
public class Nylg541 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(n-- > 0) {
int m = sc.nextInt();
BigInteger result = sovle(m);
System.out.println(result);
}
}
private static BigInteger sovle(int m) {
if(m <= 4) return BigInteger.valueOf(m);
if(m == 5) return BigInteger.valueOf(6);;
int count = m/3;
int temp = m%3;
BigInteger big = BigInteger.valueOf(3);
if(temp == 0) {
return big.pow(count);
} else if(temp == 1) {
return big.pow(count-1).multiply(BigInteger.valueOf(4));
} else {
return big.pow(count).multiply(BigInteger.valueOf(2));
}
}
}
动态规划:
import java.math.BigInteger;
import java.util.Scanner;
public class Nylg541_1 {
private static BigInteger[] dp = new BigInteger[1001];
static {
dp[1] = BigInteger.ONE;
dp[2] = BigInteger.valueOf(2);
dp[3] = BigInteger.valueOf(3);
BigInteger temp = null;
int k = 0;
for(int i=4; i<1001; i++) {
k = i >> 1;
dp[i] = BigInteger.ZERO;
for(int j=1; j<=k; j++) {
temp = dp[j].multiply(dp[i-j]);
if(temp.compareTo(dp[i]) > 0) {
dp[i] = temp;
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(n-- > 0) {
int m = sc.nextInt();
System.out.println(dp[m]);
}
}
}