公式

A - 目標スコアへの道 / Path to the Target Score 解説 by admin

GPT 5.2 High

Overview

This problem asks you to find the minimum number of coins needed when purchasing just enough score boosters to bring your current score \(A\) up to at least the required score \(B\).

Analysis

Since using \(1\) score booster increases your score by \(1\), the number needed equals the “deficit in points.”

  • If \(A \ge B\), the condition is already satisfied, so no purchase is needed, and the answer is \(0\).
  • If \(A < B\), you need \(B-A\) more points, so \(B-A\) boosters are required.
    Therefore, the number of coins is \((B-A)\times C\).

A naive implementation that “increments the score by 1 point at a time until it reaches \(B\) (using a loop)” would be correct, but since \(B-A\) can be up to \(10^9\), the number of loop iterations would be too large and exceed the time limit (causing TLE).
Since the deficit can be calculated directly with a formula, we compute it without using a loop.

Examples: - When \(A=7, B=10, C=3\): the deficit is \(10-7=3\) points → the required coins are \(3\times 3=9\). - When \(A=12, B=10, C=5\): the condition is already met → \(0\).

Algorithm

  1. Calculate the deficit \(need = \max(0, B-A)\).
  2. Output \(need \times C\) as the answer.

Complexity

  • Time complexity: \(O(1)\)
  • Space complexity: \(O(1)\)

Implementation Notes

  • To avoid negative values when \(A \ge B\), it is safe to use \(need=\max(0, B-A)\).

  • The constraints go up to \(10^9\), but the product \((B-A)\times C\) can be as large as approximately \(10^{18}\). Python’s int does not overflow, so this is not an issue.

    Source Code

import sys

def main():
    A, B, C = map(int, sys.stdin.readline().split())
    need = max(0, B - A)
    print(need * C)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.2-high.

投稿日時:
最終更新: