問題

Снимок экрана 2025-12-06 103738.png

Реши задачу на пайтоне

思考

Cross-checking rating equation logic

I’ll zoom in on a small section to verify, but the important part is the equation: he participates in n games, gaining a points for a win and losing b points for a loss. The task is to find how many wins he can have such that his rating is back to the initial value. If it’s impossible, output -1. Ok, let's sort this out.

Evaluating win count with equation constraints

I need to compute the maximum number of wins, w, where 0 ≤ w ≤ n. The equation simplifies to w = b*n / (a + b). To make sure w is valid, it must be an integer and satisfy the condition w ≤ n. Let me check if this condition holds for different values of n, a, and b.

Analyzing the edge cases and constraints

I’m confirming that the condition bnn(a+b)bn \leq n(a+b) holds true except for when n=0n = 0. For n=0n = 0, the solution is clear, w=0w = 0. The challenge is when a=0a = 0, as wins won’t change the rating. For these cases, w=nw = n, and it holds based on the equation.

Handling the special case of a = 0 and b = 0

If b=0b = 0, losses don't affect the rating, and the formula still works with w=0w = 0. However, if both aa and bb are 0, the formula becomes undefined due to the 0/00/0 division. In this case, no rating change means any combination of wins and losses is valid, but since he wants the maximum wins, w=nw = n works as the solution.

Verifying edge cases and conditions

When both a=0a = 0 and b=0b = 0, the rating remains constant, and any combination of wins and losses works. If n=0n = 0, the win count must be 0. Otherwise, I just need to check for divisibility using the formula and ensure results fit in the constraints. The solution checks divisibility and handles up to 1e91e9.

Finalizing Python code solution

The problem is straightforward in Python. Essentially, when both aa and bb are zero, the solution is w=nw = n. Otherwise, I check if the equation can be satisfied by confirming if the product b×nb \times n is divisible evenly by a+ba + b. If it’s not, the solution is “-1.”

Here’s the final code:

python
n,a,b = map(int, input().split()) if a==0 and b==0: print(n) else: s=a+b x = b*n if x % s != 0: print(-1) else: print(x//s)

Thought for 1m 1s

回答

Нужно, чтобы после всех игр рейтинг не изменился.

Пусть

  • nn — количество партий,
  • aa — очки за победу,
  • bb — очки, снимаемые за поражение,
  • ww — число побед, тогда поражений nwn-w.

Суммарное изменение рейтинга:

Δ=wa(nw)b\Delta = w \cdot a - (n-w) \cdot b

Требуется Δ=0\Delta = 0:

wa(nw)b=0(a+b)w=bnw=bna+bw a - (n-w) b = 0 \\ (a + b) w = b n \\ w = \frac{b n}{a + b}

То есть:

  • если a+b=0a+b = 0 (то есть a=b=0a = b = 0), рейтинг не меняется от любых результатов, значит можно выиграть все партии: ответ w=nw = n;
  • иначе нужно, чтобы bnb n делилось на a+ba+b. Если не делится — ответа нет, выводим 1-1.
    Если делится, то это единственное возможное число побед; оно всегда n\le n.

Код на Python:

python
n, a, b = map(int, input().split()) # Особый случай: ни победы, ни поражения не меняют рейтинг if a == 0 and b == 0: print(n) else: s = a + b total = b * n if total % s != 0: print(-1) else: w = total // s print(w)

分享這個問答