题解 LGP7909 【[CSP-J 2021] 分糖果】

posted on 2021-10-23 22:52:47 | under 题解 | source

分类讨论。

一句话题意:求 maxi=lr{imodn}

首先画个数轴,按除以 n 向下取整的商把这些整数分块,大概是这样:

容易看出每块中的整数 modn 的值单调递增,越往右越大。

接着,如果 lr 不在同一块:

那么 lr 一定会跨过一个块的右端点,所以 ans=n1

如果 lr 在同一块:

ans 最多能取到 rmodn

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
LL n,l,r;//其实不开 LL 也行
int main(){
	freopen("candy.in","r",stdin);
	freopen("candy.out","w",stdout);
	scanf("%lld%lld%lld",&n,&l,&r);
	if(l/n!=r/n) printf("%lld\n",n-1);
	else printf("%lld\n",r%n);
	return 0;
}

posted @   caijianhong  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示