Divisibility

来源:互联网 发布:数据库管理系统的特点 编辑:程序博客网 时间:2024/06/06 05:10

Divisibility
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u

Description
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 that a ≤ 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 Input
Input
1 1 10
Output
10
Input
2 -4 4
Output
5

题解:第一个数与k相除与最后一个与k相除+1即可。
(b/k-a/k)+1。
代码为:

#include<cstdio>#include<string.h>#include<algorithm>#include<math.h>int main(){    long long a,b,k,c;    while(scanf("%lld%lld%lld",&k,&a,&b)!=EOF){        long long f,l;        f=a/k;if(a>0&&a%k) ++f;        l=b/k;if(b<0&&b%k) --l;        printf("%lld\n",l-f+1);    }    return 0;}
0 0