1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
| package com.learn.normal;
import java.math.BigInteger; import java.util.ArrayList; import java.util.List;
public class HorseRaceRounds {
public static class Result { public List<BigInteger> layerGroupCounts; public BigInteger roundsForLayers; public BigInteger candidateUpperBound; public BigInteger roundsForCandidates; public BigInteger totalRounds; public String note;
public Result() { layerGroupCounts = new ArrayList<>(); roundsForLayers = BigInteger.ZERO; candidateUpperBound = BigInteger.ZERO; roundsForCandidates = BigInteger.ZERO; totalRounds = BigInteger.ZERO; note = ""; } }
public static BigInteger ceilDiv(BigInteger a, BigInteger b) { if (b.equals(BigInteger.ZERO)) { throw new ArithmeticException("Division by zero in ceilDiv"); } BigInteger numerator = a.add(b).subtract(BigInteger.ONE); return numerator.divide(b); }
public static Result computeMinRoundsUpperBound(long numHorses, long numTracks, long targetRank) { BigInteger M = BigInteger.valueOf(numHorses); BigInteger N = BigInteger.valueOf(numTracks); BigInteger K = BigInteger.valueOf(targetRank);
Result result = new Result();
if (numTracks < 2) { throw new IllegalArgumentException("numTracks (N) must satisfy (N >= 2)"); } if (numHorses < 0) { throw new IllegalArgumentException("numHorses (M) must satisfy (M >= 0)"); } if (numHorses == 0) { result.note = "no horses (M=0)"; result.layerGroupCounts = new ArrayList<>(); result.roundsForLayers = BigInteger.ZERO; result.candidateUpperBound = BigInteger.ZERO; result.roundsForCandidates = BigInteger.ZERO; result.totalRounds = BigInteger.ZERO; return result; } if (targetRank < 1 || targetRank > numHorses) { throw new IllegalArgumentException("targetRank (K) must satisfy (1 <= K <= M)"); }
if (M.compareTo(N) <= 0) { result.layerGroupCounts.add(BigInteger.ONE); result.roundsForLayers = BigInteger.ONE; result.candidateUpperBound = BigInteger.ZERO; result.roundsForCandidates = BigInteger.ZERO; result.totalRounds = result.roundsForLayers.add(result.roundsForCandidates); result.note = "M <= N: one race suffices to rank all horses"; return result; }
List<BigInteger> layerCounts = new ArrayList<>(); BigInteger powN = N; while (true) { BigInteger Gi; if (powN.compareTo(M) > 0) { Gi = BigInteger.ONE; } else { Gi = ceilDiv(M, powN); } layerCounts.add(Gi);
if (Gi.compareTo(N) <= 0) { powN = powN.multiply(N); BigInteger gNext; if (powN.compareTo(M) > 0) { gNext = BigInteger.ONE; } else { gNext = ceilDiv(M, powN); } layerCounts.add(gNext); break; } powN = powN.multiply(N); }
BigInteger sumGi = BigInteger.ZERO; for (BigInteger g : layerCounts) { sumGi = sumGi.add(g); }
BigInteger ksum = K.multiply(K.add(BigInteger.ONE)).divide(BigInteger.valueOf(2)); if (powN.compareTo(M.add(ksum)) >= 0) { ksum = BigInteger.ZERO; } BigInteger candidateUpperBound = ksum.compareTo(BigInteger.ZERO) > 0 ? ksum.subtract(BigInteger.ONE) : BigInteger.ZERO;
BigInteger roundsForCandidates = candidateUpperBound.equals(BigInteger.ZERO) ? BigInteger.ZERO : ceilDiv(candidateUpperBound, N);
result.layerGroupCounts = layerCounts; result.roundsForLayers = sumGi; result.candidateUpperBound = candidateUpperBound; result.roundsForCandidates = roundsForCandidates; result.totalRounds = sumGi.add(roundsForCandidates); result.note = "nomal result";
return result; }
public static void printResult(long M, long N, long K, Result result) { System.out.println("输入: M=" + M + " (马数), N=" + N + " (赛道数), K=" + K + " (求第 K 名)"); System.out.println("说明: " + result.note); if (result.layerGroupCounts == null || result.layerGroupCounts.isEmpty()) { System.out.println("各层组数 (layerGroupCounts): (empty)"); } else { StringBuilder sb = new StringBuilder(); for (int i = 0; i < result.layerGroupCounts.size(); ++i) { if (i > 0) { sb.append(", "); } sb.append("G").append(i + 1).append("=").append(result.layerGroupCounts.get(i).toString()); } System.out.println("各层组数 (layerGroupCounts): " + sb.toString()); } System.out.println("分层汇总所需轮数 (roundsForLayers): " + result.roundsForLayers.toString()); System.out.println("候选集合上界 S' (candidateUpperBound = K(K+1)/2 - 1): " + result.candidateUpperBound.toString()); System.out.println("候选集合分批所需轮数 (roundsForCandidates): " + result.roundsForCandidates.toString()); System.out.println("=> 至少几轮才能得到结果 (totalRounds): " + result.totalRounds.toString()); System.out.println("------------------------------------------------------------"); }
public static void main(String[] args) {
long[][] tests = { {25, 5, 3}, {64, 8, 4}, {36, 6, 6}, {30, 5, 3}, {9, 10, 4}, {5, 5, 3}, {6, 5, 2}, {0, 5, 1}, {12, 4, 1}, };
for (long[] tc : tests) { long M = tc[0], N = tc[1], K = tc[2]; try { Result b = computeMinRoundsUpperBound(M, N, K); printResult(M, N, K, b); } catch (Exception e) { System.out.println("测试 (M=" + M + ", N=" + N + ", K=" + K + ") 发生异常: " + e.getMessage()); System.out.println("------------------------------------------------------------"); } } } }
|