赞
踩
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 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 Input
1 1 10
10
2 -4 4
5
- //A了
- #include <stdio.h>
- #include <string.h>
- #include <algorithm>
- using namespace std;
- int main()
- {
- __int64 k,a,b;
- while(scanf("%I64d%I64d%I64d",&k,&a,&b)!=EOF)
- {
- __int64 t,u;
- u=a%k;
- if(u==0)
- {
- t=a;
- }
- else
- {
- if(u<0)
- {
- u=-u;
- t=a+u;//因为负的时候是将它的余数往上加的才能是大于a的,
- }//负数应该和正数是对称的!!!
- else
- {
- t=a+(k-u);
- }
- }
- if(t<=b)
- {
- __int64 sum=1;
- sum+=(b-t)/k;
- printf("%I64d\n",sum);
- }
- else
- {
- printf("0\n");
- }
- }
- return 0;
- }
- #include <stdio.h>
- #include <string.h>
- #include <algorithm>
- #define INF 1000000000000000000
- using namespace std;
- int main()
- {
- __int64 k,a,b;
- while(scanf("%I64d%I64d%I64d",&k,&a,&b)!=EOF)
- {
- __int64 sum;
- if(a>0&&b>0)
- sum=b/k-(a-1)/k;
- else if(a<0&&b<0)
- {
- sum=-1*a/k-(-1*b-1)/k;
- }
- else if(a<=0&&b>=0)
- {
- sum=-1*a/k+1+b/k;
- }
- printf("%I64d\n",sum);
- }
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。