题目 › usaco-1539

Chip Exchange

USACO 2026 First · 铜组 · 2026-01 · L1 基础 · Python 3.11 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

来源:USACO 官网。模型收到的是同一份题面的文字。

Bessie the cow has in her possession $A$ chips of type A and $B$ chips of type B ($0\le A,B\le 10^9$). She can perform the following operation as many times as she likes:

  • If you have at least $c_B$ chips of type B, exchange $c_B$ chips of type B for $c_A$ chips of type A ($1\le c_A,c_B\le 10^9$).

Determine the minimum non-negative integer $x$ such that the following holds: after receiving $x$ additional random chips, it is guaranteed that Bessie can end up with at least $f_A$ chips of type A ($0\le f_A\le 10^9$).

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains $T$, the number of independent test cases ($1\le T\le 10^4$).

Then follow $T$ tests, each consisting of five integers $A,B,c_A,c_B,f_A$.

OUTPUT FORMAT (print output to the terminal / stdout):

Output the answer for each test on a separate line.

Note: The large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).

SAMPLE INPUT:

2
2 3 1 1 6
2 3 1 1 4

SAMPLE OUTPUT:

1
0

SAMPLE INPUT:

5
0 0 2 3 5
0 1 2 3 5
1 0 2 3 5
10 10 2 3 5
0 0 1 1000000000 1000000000

SAMPLE OUTPUT:

9
8
7
0
1000000000000000000

For the first test, Bessie initially starts with no chips. If she receives any $9$ additional chips, she can perform the operation to end up with at least $5$ chips of type A. For example, if she receives $2$ chips of type A and $7$ chips of type B, she can perform the operation twice to end up with $6\ge 5$ chips of type A. However, if she only receive $8$ chips of type B, she can only end up with $4<5$ chips of type A.

For the fourth test, she already has enough chips of type A from the start.

SCORING:

  • Input 3: $c_A=c_B=1$
  • Inputs 4-5: $x\le 10$ for all cases
  • Inputs 6-7: $c_A=2$, $c_B=3$
  • Inputs 8-12: No additional constraints.

Problem credits: Benjamin Qi

结果

Kimi K3.1 ProKimi

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 13.9k平均输出 token(含思考) ↓
  • 31.3输出速度 tokens/s ↑
  • 447 秒延迟 ↓
  • 5.22×CPU 时间 / 官方解 ↓
  • 不可测时间复杂度(相对官方解)
  • 27Pylint 问题 ↓
  • 3.57Pylint 得分 ↑
  • 7圈复杂度 ↓
  • 65.1可维护性指数 ↑

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 14.8k平均输出 token(含思考) ↓
  • 208.7输出速度 tokens/s ↑
  • 71 秒延迟 ↓
  • 6.19×CPU 时间 / 官方解 ↓
  • 不可测时间复杂度(相对官方解)
  • 18Pylint 问题 ↓
  • 5.38Pylint 得分 ↑
  • 10圈复杂度 ↓
  • 65.8可维护性指数 ↑

MiniMax M3.1 FlashMiniMax

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 18.1k平均输出 token(含思考) ↓
  • 121.1输出速度 tokens/s ↑
  • 150 秒延迟 ↓
  • 0.55×CPU 时间 / 官方解 ↓
  • 更优时间复杂度(相对官方解)
  • 7Pylint 问题 ↓
  • 7.50Pylint 得分 ↑
  • 6圈复杂度 ↓
  • 60.8可维护性指数 ↑

↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 66 行
import sys


def solve():
    data = list(map(int, sys.stdin.buffer.read().split()))
    if not data:
        return

    T = data[0]
    pos = 1
    answers = []

    for _ in range(T):
        A, B, c_A, c_B, f_A = data[pos:pos + 5]
        pos += 5

        def guaranteed(x):
            # If t is the total number of B chips before exchanges, then
            # t ranges from B to C, and the final number of A chips is
            # A + C - H(t), where H(t) = t - c_A * floor(t / c_B).
            C = B + x

            max_h = C - c_A * (C // c_B)

            # H(t) increases within each block of c_B consecutive values.
            # Thus, besides C, only one block endpoint needs consideration.
            if c_A <= c_B:
                # Largest t <= C with t % c_B == c_B - 1.
                t = C - (C + 1) % c_B
                if t >= B:
                    h = t - c_A * (t // c_B)
                    if h > max_h:
                        max_h = h
            else:
                # Smallest t >= B with t % c_B == c_B - 1.
                t = B + ((-(B + 1)) % c_B)
                if t <= C:
                    h = t - c_A * (t // c_B)
                    if h > max_h:
                        max_h = h

            return A + C - max_h >= f_A

        if guaranteed(0):
            answers.append("0")
            continue

        lo, hi = 0, 1
        while not guaranteed(hi):
            lo = hi
            hi *= 2

        while hi - lo > 1:
            mid = (lo + hi) // 2
            if guaranteed(mid):
                hi = mid
            else:
                lo = mid

        answers.append(str(hi))

    sys.stdout.write("\n".join(answers))


if __name__ == "__main__":
    solve()