|
|
@ -2,72 +2,78 @@ package class22;
|
|
|
|
|
|
|
|
|
|
|
|
public class Code01_KillMonster {
|
|
|
|
public class Code01_KillMonster {
|
|
|
|
|
|
|
|
|
|
|
|
public static double right1(int N, int M, int K) {
|
|
|
|
public static double right(int N, int M, int K) {
|
|
|
|
if (N < 1 || M < 1 || K < 1) {
|
|
|
|
if (N < 1 || M < 1 || K < 1) {
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
long all = (long) Math.pow(M + 1, K);
|
|
|
|
long all = (long) Math.pow(M + 1, K);
|
|
|
|
long kill = process1(N, M, K);
|
|
|
|
long kill = process(K, M, N);
|
|
|
|
return (double) ((double) kill / (double) all);
|
|
|
|
return (double) ((double) kill / (double) all);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static long process1(int N, int M, int K) {
|
|
|
|
// 怪兽还剩hp点血
|
|
|
|
if (K == 0) {
|
|
|
|
// 每次的伤害在[0~M]范围上
|
|
|
|
return N <= 0 ? 1 : 0;
|
|
|
|
// 还有times次可以砍
|
|
|
|
|
|
|
|
// 返回砍死的情况数!
|
|
|
|
|
|
|
|
public static long process(int times, int M, int hp) {
|
|
|
|
|
|
|
|
if (times == 0) {
|
|
|
|
|
|
|
|
return hp <= 0 ? 1 : 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hp <= 0) {
|
|
|
|
|
|
|
|
return (long) Math.pow(M + 1, times);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
long ways = 0;
|
|
|
|
long ways = 0;
|
|
|
|
for (int i = 0; i <= M; i++) {
|
|
|
|
for (int i = 0; i <= M; i++) {
|
|
|
|
ways += process1(N - i, M, K - 1);
|
|
|
|
ways += process(times - 1, M, hp - i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ways;
|
|
|
|
return ways;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static double right2(int N, int M, int K) {
|
|
|
|
public static double dp1(int N, int M, int K) {
|
|
|
|
if (N < 1 || M < 1 || K < 1) {
|
|
|
|
if (N < 1 || M < 1 || K < 1) {
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
long all = (long) Math.pow(M + 1, K);
|
|
|
|
long all = (long) Math.pow(M + 1, K);
|
|
|
|
long kill = process2(N, M, K);
|
|
|
|
long[][] dp = new long[K + 1][N + 1];
|
|
|
|
return (double) ((double) kill / (double) all);
|
|
|
|
dp[0][0] = 1;
|
|
|
|
}
|
|
|
|
for (int times = 1; times <= K; times++) {
|
|
|
|
|
|
|
|
dp[times][0] = (long) Math.pow(M + 1, times);
|
|
|
|
public static long process2(int N, int M, int K) {
|
|
|
|
for (int hp = 1; hp <= N; hp++) {
|
|
|
|
if (K == 0) {
|
|
|
|
long ways = 0;
|
|
|
|
return N <= 0 ? 1 : 0;
|
|
|
|
for (int i = 0; i <= M; i++) {
|
|
|
|
}
|
|
|
|
if (hp - i >= 0) {
|
|
|
|
if (N <= 0) {
|
|
|
|
ways += dp[times - 1][hp - i];
|
|
|
|
return (long) (Math.pow(M + 1, K));
|
|
|
|
} else {
|
|
|
|
|
|
|
|
ways += (long) Math.pow(M + 1, times - 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
dp[times][hp] = ways;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return process2(N, M, K - 1) + process2(N - 1, M, K) - process2(N - M - 1, M, K - 1);
|
|
|
|
long kill = dp[K][N];
|
|
|
|
|
|
|
|
return (double) ((double) kill / (double) all);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// M = 5
|
|
|
|
public static double dp2(int N, int M, int K) {
|
|
|
|
// 以下为斜率优化改进枚举行为的动态规划
|
|
|
|
|
|
|
|
// dp[8][5] = dp[8..3][4]
|
|
|
|
|
|
|
|
// dp[9][5] = dp[9..4][4] = dp[9][4] + dp[8][5] - dp[3][4]
|
|
|
|
|
|
|
|
// 可以推出
|
|
|
|
|
|
|
|
// dp[i][j] = dp[i][j-1] + dp[i-1][j] - dp[i - M - 1][j-1]
|
|
|
|
|
|
|
|
public static double dp(int N, int M, int K) {
|
|
|
|
|
|
|
|
if (N < 1 || M < 1 || K < 1) {
|
|
|
|
if (N < 1 || M < 1 || K < 1) {
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
long[][] dp = new long[N + 1][K + 1];
|
|
|
|
long all = (long) Math.pow(M + 1, K);
|
|
|
|
// 特别注意:dp[0][j]既表示原含义,也表示M+1的j次方的值
|
|
|
|
long[][] dp = new long[K + 1][N + 1];
|
|
|
|
dp[0][0] = 1;
|
|
|
|
dp[0][0] = 1;
|
|
|
|
for (int j = 1; j <= K; j++) {
|
|
|
|
for (int times = 1; times <= K; times++) {
|
|
|
|
dp[0][j] = dp[0][j - 1] * (M + 1);
|
|
|
|
dp[times][0] = (long) Math.pow(M + 1, times);
|
|
|
|
}
|
|
|
|
for (int hp = 1; hp <= N; hp++) {
|
|
|
|
for (int j = 1; j <= K; j++) {
|
|
|
|
dp[times][hp] = dp[times][hp - 1] + dp[times - 1][hp];
|
|
|
|
for (int i = 1; i <= N; i++) {
|
|
|
|
if (hp - 1 - M >= 0) {
|
|
|
|
dp[i][j] = dp[i][j - 1] + dp[i - 1][j];
|
|
|
|
dp[times][hp] -= dp[times - 1][hp - 1 - M];
|
|
|
|
if (i - M - 1 < 0) {
|
|
|
|
|
|
|
|
dp[i][j] -= dp[0][j - 1];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
dp[i][j] -= dp[i - M - 1][j - 1];
|
|
|
|
dp[times][hp] -= Math.pow(M + 1, times - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (double) ((double) dp[N][K] / (double) dp[0][K]);
|
|
|
|
long kill = dp[K][N];
|
|
|
|
|
|
|
|
return (double) ((double) kill / (double) all);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
public static void main(String[] args) {
|
|
|
@ -80,9 +86,9 @@ public class Code01_KillMonster {
|
|
|
|
int N = (int) (Math.random() * NMax);
|
|
|
|
int N = (int) (Math.random() * NMax);
|
|
|
|
int M = (int) (Math.random() * MMax);
|
|
|
|
int M = (int) (Math.random() * MMax);
|
|
|
|
int K = (int) (Math.random() * KMax);
|
|
|
|
int K = (int) (Math.random() * KMax);
|
|
|
|
double ans1 = right1(N, M, K);
|
|
|
|
double ans1 = right(N, M, K);
|
|
|
|
double ans2 = right2(N, M, K);
|
|
|
|
double ans2 = dp1(N, M, K);
|
|
|
|
double ans3 = dp(N, M, K);
|
|
|
|
double ans3 = dp2(N, M, K);
|
|
|
|
if (ans1 != ans2 || ans1 != ans3) {
|
|
|
|
if (ans1 != ans2 || ans1 != ans3) {
|
|
|
|
System.out.println("Oops!");
|
|
|
|
System.out.println("Oops!");
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|