Testing Round #12 A

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

不得不说,这题的思路和上一个一样的
不妨我们考虑多点,[a,b]可以整除的个数  
a<0 b<0     (-a)/k-(-b)/k 考虑b是否整除
a<0 b=0     (-a)/k;  加0本身
a<0 b>0     (b)/k+(-a)/k 加0本身
a=0 b>0     (b)/k  加0本身
a>0 b>0     (b)/k-(a)/k  考虑a是否整除
a=0 b=0      0本身
#include<stdio.h>
//#include<bits/stdc++.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<sstream>
#include<set>
#include<queue>
#include<vector>
#include<algorithm>
#include<limits.h>
#define inf 0x3fffffff
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
using namespace std;
int main()
{
    LL k,a,b;
    LL ans=0;
    cin>>k>>a>>b;
    if(a<0&&b<0)
    {
       ans+=(-a)/k-(-b)/k;
       if((-b)%k==0)
       {
           ans++;
       }
    }
    else if(a<0&&b==0)
    {
       ans+=(-a)/k;
       ans++;
    }
    else if(a<0&&b>0)
    {
        ans+=(b)/k+(-a)/k;
        ans++;
    }
    else if(a==0&&b>0)
    {
        ans+=(b)/k;
        ans++;
    }
    else if(a>0&&b>0)
    {
        ans+=(b)/k-(a)/k;
        if(a%k==0)
        {
            ans++;
        }
    }
    else if(a==0&&b==0)
    {
        ans++;
    }
    cout<<ans<<endl;
    return 0;
}

  

原文地址:https://www.cnblogs.com/yinghualuowu/p/4996070.html