【Codeforces Testing Round 12A】【讨论 边界元素映射】Divisibility 区间范围内k倍数的数的个数

A. Divisibility
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x thata ≤ x ≤ b and x is divisible by k.

Input

The only line contains three space-separated integers ka and b (1 ≤ k ≤ 1018; - 1018 ≤ a ≤ b ≤ 1018).

Output

Print the required number.

Sample test(s)
input
1 1 10
output
10
input
2 -4 4
output
5


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T> inline void gmax(T &a,T b){if(b>a)a=b;}
template <class T> inline void gmin(T &a,T b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
LL a,b,k;
int main()
{
	while(~scanf("%lld%lld%lld",&k,&a,&b))
	{
		LL lft=a/k;if(a>0&&a%k)++lft;
		LL rgt=b/k;if(b<0&&b%k)--rgt;
		printf("%lld\n",rgt-lft+1);
	}
	return 0;
}
/*
【题意】
给你一个模数k(1<=k<=1e18),
然后再给你一个区间[a,b](-1e18<=a<=b<=1e18),
问你这个区间内有多少个数,使得这个数为k的倍数。

【类型】
讨论

【分析】
第一个想法是,我们求出区间长度a-b+1,然后看这个区间长度是k的几倍。
然而,问题很显然。比如说k=5,我们一个长度为6的区间内,k倍数的数可能是1个或2个。
对于区间[0,5],k倍数的数为2个;对于区间[1,6],k倍数的数只有1个。
于是我们发现,我们还需要特殊判定,处于边界位置的k倍数的数。

既然如此,我们不妨直接映射到边界位置的k倍数的数——
LL lft=a/k;if(a>0&&a%k)++lft;//最左的第一个数是lft
LL rgt=b/k;if(b<0&&b%k)--rgt;//最右的第一个数是rgt
printf("%lld\n",rgt-lft+1);//答案就是rgt-lft+1

做完喽!边界映射的思想棒棒哒!

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值