Official

A - 羊の数え直し / Recounting the Sheep Editorial by admin

gpt-6-astra-medium

Overview

Determine whether the total number of sheep in the morning, \(A+B\), is equal to the number of sheep in the evening, \(C\). Print Yes if they are equal, and No otherwise.

Analysis

In the morning, the sheep are divided into \(2\) flocks, so the total number of sheep is \(A+B\). If the number of sheep has not increased or decreased, the total count in the evening should be the same, so:

\(A+B=C\)

holds.

For example, if \(A=3\), \(B=4\), and \(C=7\), there are \(7\) sheep in both the morning and evening, so the answer is Yes. If \(C=6\), the counts differ between morning and evening, so the answer is No.

This problem can be sufficiently solved by directly comparing the sums. There is no need to simulate counting the sheep one by one. However, comparing only \(A\) or only \(B\) with \(C\) will ignore the other flock and result in an incorrect answer.

In the provided code, instead of a direct equality comparison, the difference

\(\mathrm{diff}=C-A-B=C-(A+B)\)

is calculated. This difference being \(0\) is equivalent to the morning and evening totals being equal.

Algorithm

  1. Read the integers \(A,B,C\).
  2. Compute \(\mathrm{diff}=C-A-B\).
  3. If \(\mathrm{diff}=0\), output Yes; otherwise, output No.

Since the difference is \(0\) if and only if \(C=A+B\), this procedure correctly determines the answer.

Complexity

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

Regardless of the input values, only a constant number of calculations and comparisons are performed, and the number of variables used is also constant.

Implementation Notes

  • From the constraints, \(A+B\) is at most \(2000\), which can be safely handled by the C++ int type.

  • In the code, diff == 0 ? "Yes" : "No" uses the conditional operator to choose "Yes" if the difference is \(0\), and "No" otherwise.

  • The output is case-sensitive, so make sure to output Yes or No exactly as specified.

    Source Code

// 問題文から独立に実装: 差が 0 かどうか
#include <iostream>
int main() {
    int a, b, c; std::cin >> a >> b >> c;
    int diff = c - a - b;
    std::cout << (diff == 0 ? "Yes" : "No") << std::endl;
}

This editorial was generated by gpt-6-astra-medium.