公式

A - 落葉の掃除 / Fallen Leaves Cleaning 解説 by admin 印刷ページ

gpt-6-astra-medium

Overview

The total number of fallen leaves is \(A \times B\). We simulate cleaning up to \(C\) leaves per day and determine whether all leaves can be cleaned within \(D\) days.

Analysis

To finish cleaning as early as possible, we should clean the maximum possible amount of up to \(C\) remaining leaves each day.

Therefore, let leaves be the remaining number of leaves, initialized to \(A \times B\). Each day we clean, we subtract \(C\) from leaves, and once the remaining count becomes \(0\) or less, we determine that all leaves have been cleaned.

For example, when \(A=3,\ B=5,\ C=4,\ D=4\), there are \(15\) leaves in total. The remaining count changes as follows:

  • After 1 day: \(11\)
  • After 2 days: \(7\)
  • After 3 days: \(3\)
  • After 4 days: \(-1\)

In reality, only the remaining \(3\) leaves are cleaned on the last day, but the code uniformly subtracts \(C\). For this reason, a negative remaining count is also treated as completion.

Since \(D \leq 1000\) in this problem, a naive day-by-day simulation is well within the time limit. However, note that if the completion condition is set to “the remainder is exactly \(0\),” it will result in a Wrong Answer for the example above.

Additionally, since the maximum number of leaves that can be cleaned in \(D\) days is \(C \times D\), one can also directly check whether \(A \times B \leq C \times D\). The code presented here performs the day-by-day simulation.

Algorithm

  1. Read \(A,B,C,D\).
  2. Set the remaining leaves leaves to \(A \times B\).
  3. While both of the following conditions are met, subtract \(C\) from leaves and increment the elapsed days by 1:
    • The elapsed days is less than \(D\).
    • leaves is greater than \(0\).
  4. If leaves is \(0\) or less, output Yes; otherwise, output No.

If leaves still remain after \(D\) days despite cleaning the maximum possible amount every day, cleaning any less would not finish either, so this check gives the correct answer.

Complexity

  • Time complexity: \(O(D)\). The loop runs at most \(D\) times.
  • Space complexity: \(O(1)\). Only a fixed number of variables are used.

Implementation Notes

  • The condition to check completion should be leaves <= 0, not leaves == 0.

  • Once cleaning is completed midway, further simulation is unnecessary. The loop terminates early due to the condition leaves > 0.

  • By initializing day to \(0\) and using the condition day < d, the cleaning is performed at most \(D\) times.

  • As specified, the output is case-sensitive: output either Yes or No.

    Source Code

// 問題文から独立に実装: 1 日ずつ掃除して残りが 0 以下になるか
#include <iostream>
int main() {
    long long a, b, c, d; std::cin >> a >> b >> c >> d;
    long long leaves = a * b;
    for (int day = 0; day < d && leaves > 0; ++day) leaves -= c;
    std::cout << (leaves <= 0 ? "Yes" : "No") << std::endl;
}

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

投稿日時:
最終更新: