Editorial for Flow God và n em gái


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

Spoiler Alert


Solve

  • Gọi (\(mx\)) là hằng số chỉ số lượng viên kẹo lớn nhất một em gái có được
  • Đầu tiên mình sẽ phát kẹo sao cho (\(n + 1\)) em gái đều nhận (\(mx\)) viên kẹo \(= (mx) * (n + 1)\)
  • Trước đó đã có một lượng (\(sum\)) viên kẹo

Sau khi phát thì số viên kẹo còn lại là \((new_k) = (k) - (mx) * (n + 1) + (sum)\) viên

Nếu \((new_k) < 0\) (tức là không đủ viên để phát) -> "NO"

  • Thử phát tiếp (\(new_k\)) viên kẹo còn lại cho (\(n + 1\)) người.
  • Vì mọi người phải bình đẳng nên nếu \((new_k) = (m) * (n + 1)\) thì mọi người đều có \((mx) + (m)\) viên kẹo

\((new_k) % (n + 1) = 0\) thì in "YES" không thì in "NO"


Reference AC code | \(O(n \times q)\) time | \(O(1)\) auxiliary space | Math

C++
int query()
{
    int n = readInt();
    ll k = readInt();

    ll sum = 0;
    int mx = 0;
    for (int i = 0; i < n; ++i)
    {
        int x = readInt();
        mx = max(mx, x);
        sum += x;
    }

    k -= 1LL * mx * (n + 1) - sum;
    if (k < 0) return cout << "NO\n", 0;
    cout << (k % (n + 1) == 0 ? "YES\n" : "NO\n");
    return 0;
}


Comments

There are no comments at the moment.