跳到正文

题解

P7909 [CSP-J 2021] 分糖果 题解

farfarqwq约 2 分钟
原文
文章信息与版本

文章信息

发布
2021年10月23日
收录
2026年10月14日
更新
2026年10月14日
篇幅
约 2 分钟
题目
P7909

版本

  1. v1当前

本人第一篇题解,如有不足请指正。


正题分隔线


如果想要自己无限制拿到的奖励最多,那一定就是拿 n×x−1n\times x-1 块糖了。 所以:

  • 首先判断 n×⌊r/n⌋n\times\lfloor r/n \rfloor 是不是在范围内,即: l<=n×⌊r/n⌋l <= n \times\lfloor r/n \rfloor。

    因为 n×⌊r/n⌋n\times\lfloor r/n \rfloor 是 rr 内最大的,所以只需考虑 ll 的情况就行了。

  • 但是如果不在范围内呢?简单理解一下,意思是 n×x<ln\times x<l 和 r<n×(x+1)r<n\times (x+1) ,无论如何 r−n×⌊r/n⌋r-n\times\lfloor r/n \rfloor 是最大的。

其他情况不用考虑,直接上代码:

C++
#include <bits/stdc++.h>
using namespace std;
int main() {
//    freopen("candy.in", "r", stdin);
//    freopen("candy.out", "w", stdout);
    int n, l, r;
    cin >> n >> l >> r;
    if (n * (r / n) - 1 >= l) //情况1
    {
        cout << n - 1;
    }
    else //情况2
    {
        cout << r - n * (r / n);
    }
    return 0;
}

由于c++自动向下取整,这道题轻松水过。

珍爱生命,远离抄袭 ,虽然这是入门难度的题。


最后,祝一年后的朋友们CSP2022 RP++!

原文