题目描述:
You are given nonnegative integers and ( a <= b), and a positive integer . Among the integers between a and b , inclusive, how many are divisible by X?
0 <= a <= b <= 1e18;
1 <= x <= 1e18
inupt
a b x
output
Print the number of the integers between a and b, inclusive, that are divisible by x.
样例输入:
4 8 2
样例输出
3
样例输入
0 5 1
6
样例输出:
6
样例输入
9 9 2
样例输出
0
样例输入
1 1000000000000000000 3
样例输出
333333333333333333
样例输入
6 17 4
样例输出
3
解题思路,一般求能被某个数整除的数的个数直接循环比较就可以,但是这里题目里面说取值范围为1e18,但计算机一般计算时间为1e6次/秒,相对1e18的数据肯定很容易就爆了,所以肯定不能用循环去做;
所以我们可以先求出a以内能被x整除的数的个数,在用b以内能被x整除的数的个数减去,就可以得到a-b内能被x整除的数的个数。
上代码
#include <iostream>
#include <cstdio>
#include <string>
#include <queue>
using namespace std;
#define ll long long
int main(){
ll a,b,x,ans = 0;
cin >> a >> b >> x;
if(a%x == 0)//如果a能被想整除那么我们就要在结果上加1,因为如果a能被整除,那么做减法的时候我们就会把a这个数减掉,到值最后结果少1
ans = b/x-a/x + 1;
else //如果不能整除则不管
ans = b/x-a/x;
cout << ans << "\n";
return 0;
}