luogu#P7909. [CSP-J 2021] 分糖果

[CSP-J 2021] 分糖果

Background

The children at Red Sun Kindergarten are starting to share candies.

Problem Description

There are nn children at Red Sun Kindergarten, and you are one of them. It is guaranteed that n2n \ge 2.

One day, you find infinitely many candies in the back garden of the kindergarten, and you plan to take some candies back to share with the children.

However, since you are just an ordinary kindergarten child, your strength is limited, and you can carry at most RR candies back.

But carrying too few is not enough to share, so you must carry at least LL candies back. It is guaranteed that nLRn \le L \le R.

That is, if you take kk candies, you must ensure that LkRL \le k \le R.

If you take kk candies, you will put these kk candies into a basket, and require everyone to share candies as follows: as long as there are at least nn candies in the basket, all nn children in the kindergarten (including yourself) each take exactly one candy from the basket, until the number of candies in the basket becomes less than nn. At that point, all remaining candies in the basket belong to you — these candies are your reward for carrying the candies.

As a high-quality kindergarten child, you want the number of candies you get as your reward for carrying the candies ( not the total number of candies you finally get ) to be as large as possible. Therefore, you need to write a program that reads n,L,Rn, L, R and outputs the maximum number of candies you can obtain as your reward for carrying the candies.

Input Format

One line contains three positive integers n,L,Rn, L, R, representing the number of children, and the lower and upper bounds on the number of candies.

Output Format

Output one line containing one integer, representing the maximum number of candies you can obtain as your reward for carrying the candies.

7 16 23

6

10 14 18

8

见附件中的 candy/candy3.in。
见附件中的 candy/candy3.ans。

Hint

Sample Explanation #1

Take k=20k = 20 candies and put them into the basket.

The basket now has 20n=720 \ge n = 7 candies, so all children each get one candy.

The basket now has 13n=713 \ge n = 7 candies, so all children each get one candy.

The basket now has 6<n=76 < n = 7 candies, so these 66 candies are your reward for carrying the candies.

It is easy to see that the number of candies you get as your reward for carrying the candies cannot exceed 66 (otherwise, the basket would still have at least nn candies in the end, and everyone would need to take one more candy). Therefore, the answer is 66.

Sample Explanation #2

It is easy to see that when the number of candies you take kk satisfies 14=LkR=1814 = L \le k \le R = 18, after all children each take one candy, the remaining k10k - 10 candies are always the number of candies as your reward for carrying the candies. Therefore, taking k=18k = 18 is optimal, and the answer is 88.

Constraints

Test Point nn \le RR \le RLR - L \le
11 22 55
22 55 1010
33 103{10}^3
44 105{10}^5
55 103{10}^3 109{10}^9 00
66 103{10}^3
77 105{10}^5 105{10}^5
88 109{10}^9 109{10}^9
99
1010

For all testdata, it is guaranteed that 2nLR1092 \le n \le L \le R \le {10}^9.

【Thanks for providing hack data】
wangbinfeng

Translated by ChatGPT 5