4140: 分糖果(candy)

内存限制:512 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:5 解决:2

题目描述

【题目背景】
红太阳幼儿园的小朋友们开始分糖果啦!
【题目描述】
红太阳幼儿园有 lns="http://www.w3.org/1998/Math/MathML"> 个小朋友,你是其中之一。保证 lns="http://www.w3.org/1998/Math/MathML"> ≥ 2。
有一天你在幼儿园的后花园里发现无穷多颗糖果,你打算拿一些糖果回去分给幼儿园的小朋友们。
由于你只是个平平无奇的幼儿园小朋友,所以你的体力有限,至多只能拿 lns="http://www.w3.org/1998/Math/MathML"> 块糖回去。
但是拿的太少不够分的,所以你至少要拿 lns="http://www.w3.org/1998/Math/MathML"> 块糖回去。保证 lns="http://www.w3.org/1998/Math/MathML"> ≤ lns="http://www.w3.org/1998/Math/MathML"> ≤ lns="http://www.w3.org/1998/Math/MathML">
也就是说,如果你拿了 lns="http://www.w3.org/1998/Math/MathML"> 块糖,那么你需要保证 lns="http://www.w3.org/1998/Math/MathML"> ≤ lns="http://www.w3.org/1998/Math/MathML"> ≤ lns="http://www.w3.org/1998/Math/MathML">
如果你拿了 lns="http://www.w3.org/1998/Math/MathML"> 块糖,你将把这 lns="http://www.w3.org/1998/Math/MathML"> 块糖放到篮子里,并要求大家按照如下方案分糖果:只要篮子里有不少于 lns="http://www.w3.org/1998/Math/MathML"> 块糖果,幼儿园的所有 lns="http://www.w3.org/1998/Math/MathML"> 个小朋友(包括你自己)都从篮子中拿走恰好一块糖,直到篮子里的糖数量少于 lns="http://www.w3.org/1998/Math/MathML"> 块。此时篮子里剩余的糖果均归你所有——这些糖果是作为你搬糖果的奖励
作为幼儿园高质量小朋友,你希望让作为你搬糖果的奖励的糖果数量(而不是你最后获得的总糖果数量!)尽可能多;因此你需要写一个程序,依次输入 lns="http://www.w3.org/1998/Math/MathML">lns="http://www.w3.org/1998/Math/MathML">lns="http://www.w3.org/1998/Math/MathML">,并输出出你最多能获得多少作为你搬糖果的奖励的糖果数量。

输入

输入一行,包含三个正整数 lns="http://www.w3.org/1998/Math/MathML">lns="http://www.w3.org/1998/Math/MathML">lns="http://www.w3.org/1998/Math/MathML">,分别表示小朋友的个数、糖果数量的下界和上界。

【数据范围】

对于所有数据,保证 2 ≤ lns="http://www.w3.org/1998/Math/MathML"> ≤ lns="http://www.w3.org/1998/Math/MathML"> ≤ lns="http://www.w3.org/1998/Math/MathML"> ≤ 109

输出

输出一行一个整数,表示你最多能获得的作为你搬糖果的奖励的糖果数量。

样例输入 复制

7 16 23

样例输出 复制

6

提示

【样例 1 解释】
拿 lns="http://www.w3.org/1998/Math/MathML"> = 20 块糖放入篮子里。
篮子里现在糖果数 lns="http://www.w3.org/1998/Math/MathML">20=7,因此所有小朋友获得一块糖;
篮子里现在糖果数变成 lns="http://www.w3.org/1998/Math/MathML">13=7,因此所有小朋友获得一块糖;
篮子里现在糖果数变成 lns="http://www.w3.org/1998/Math/MathML">6<=7,因此这 6 块糖是作为你搬糖果的奖励
容易发现,你获得的作为你搬糖果的奖励的糖果数量不可能超过 6 块(不然,篮子里的糖果数量最后仍然不少于 lns="http://www.w3.org/1998/Math/MathML">,需要继续每个小朋友拿一块),因此答案是 6。

【样例 2 解释】
容易发现,n=10,当你拿的糖数量 lns="http://www.w3.org/1998/Math/MathML"> 满足 14 = lns="http://www.w3.org/1998/Math/MathML"> ≤ lns="http://www.w3.org/1998/Math/MathML"> ≤ lns="http://www.w3.org/1998/Math/MathML"> = 18 时,所有小朋友获得一块糖后,剩下的 lns="http://www.w3.org/1998/Math/MathML"> − 10 块糖总是作为你搬糖果的奖励的糖果数量,因此拿 lns="http://www.w3.org/1998/Math/MathML"> = 18 块是最优解,答案是 8。


#include<bits/stdc++.h>
using namespace std;
//解法1:暴力解法-时间复杂度O(n) 
int main(){
    int n,l,r;cin>>n>>l>>r;
    int ans=0;
    for(int i=l;i<=r;i++) {
    	ans=max(ans,i%n);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
//解法2:数学解法-时间复杂度O(1) 
int main(){
    int n,l,r;cin>>n>>l>>r;
    if (l/n==r/n) {
    	cout<<r%n<<endl;
	}else {
		cout<<n-1;
	} 
	return 0;
}