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

来源:互联网 发布:淘宝卖包包店铺名字 编辑:程序博客网 时间:2024/06/08 11:05
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 k, a 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|1typedef 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;//最左的第一个数是lftLL rgt=b/k;if(b<0&&b%k)--rgt;//最右的第一个数是rgtprintf("%lld\n",rgt-lft+1);//答案就是rgt-lft+1做完喽!边界映射的思想棒棒哒!*/


0 0