You are given nonnegative integers a and b

题目描述:
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值