A - 証明書の交付 / Issuing Certificates 解説 by admin
gpt-6-astra-mediumOverview
Determine whether it is possible to purchase \(N\) certificates, each costing \(A\) yen, with an initial budget of \(B\) yen. The presented code simulates paying the fee for each certificate one by one.
Analysis
Each time you receive a certificate, you pay \(A\) yen from your remaining money. Therefore, just before each payment, we can determine the following based on the remaining balance:
- If the remaining money is strictly less than \(A\) yen, you cannot pay for the next certificate, so the answer is
No. - If the remaining money is at least \(A\) yen, subtract \(A\) yen and proceed to the next payment.
If this can be repeated \(N\) times, all fees have been paid, so the answer is Yes.
For example, if \(N=3,\ A=200,\ B=500\), the remaining money changes as \(500 \to 300 \to 100\) yen. Since there is not enough to cover the \(200\) yen needed for the 3rd certificate, the answer is No.
In this problem, since \(N \leq 100\), this naive approach of paying one by one is fast enough and will not cause a TLE. Note that if the remaining money is exactly \(A\) yen, you can still pay, so making the condition “less than or equal to \(A\) yen” would lead to a wrong answer.
Note that since the total cost is \(N \times A\) yen, this problem can also be solved simply by checking whether \(N \times A \leq B\).
Algorithm
- Read \(N, A, B\), and initialize a variable
moneyrepresenting the remaining balance with \(B\). - Repeat the following process \(N\) times:
- If
moneyis strictly less than \(A\), outputNoand terminate the program. - Otherwise, subtract \(A\) from
money.
- If
- If all \(N\) payments are completed without early termination, output
Yes.
Since we check the remaining balance before each payment, we will always output No if a payment cannot be made. Furthermore, if the loop runs to the end, it means all \(N\) certificates were successfully paid for, correctly concluding with Yes.
Complexity
- Time Complexity: \(O(N)\)
We perform the check and update the remaining balance at most \(N\) times. - Space Complexity: \(O(1)\)
Only a constant number of variables are used, such as the remaining balance and loop counters.
Implementation Notes
The condition for being unable to pay is
money < a. Ifmoney == a, the payment is still possible.It is completely fine if the remaining balance becomes \(0\) yen after all payments.
When printing
No, terminate immediately usingreturn 0;. This prevents an extraYesfrom being printed at the end.Ensure that the output strictly matches the required case, printing
YesorNo.Source Code
// 問題文から独立に実装: 1枚ずつ払っていき、途中で足りなくなったら No
#include <iostream>
int main() {
int n, a, b; std::cin >> n >> a >> b;
int money = b;
for (int i = 0; i < n; ++i) {
if (money < a) { std::cout << "No" << std::endl; return 0; }
money -= a;
}
std::cout << "Yes" << std::endl;
}
This editorial was generated by gpt-6-astra-medium.