题目:http://codeforces.com/problemset/problem/597/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 that a ≤ 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.

Examples
input

Copy
1 1 10
output

Copy
10
input

Copy
2 -4 4
output

Copy
5

题意:

给一个区间[a,b],问这个区间内有多少个数是k(k>0)的倍数

思路:

首先可以注意到0必定是k的倍数
若用x/k,则得到(0,x](x>0)或[x,0)(x<0)这个区间内有多少个k的倍数,这相当于一个前缀和(注意这里)
所以问[a,b]这个区间有多少个k的倍数时,若a>0且b>0,则ans=b/k-(a-1)/k (a-1是因为a可能是k的倍数,而b/k计算过一遍了,所以从a-1开始算,后面b+1同理),若a<0且b>0,则ans=(b+1)/k-a/k;,否则如果a<=0且b>=0则必定包含了0,所以ans=b/k-a/k+1(这里+1是为了加上0)

 #include<bits/stdc++.h>
using namespace std;
int main(){
long long k,a,b,ans;
while(cin>>k>>a>>b){
if(b<)
ans=(b+)/k-a/k;
else if(a>)
ans=b/k-(a-)/k;
else
ans=b/k-a/k+;
printf("%lld\n",ans);
}
}
/**
给一个区间[a,b],问这个区间内有多少个数是k(k>0)的倍数
首先可以注意到0必定是k的倍数
若用x/k,则得到(0,x](x>0)或[x,0)(x<0)这个区间内有多少个k的倍数,这相当于一个前缀和(注意这里)
所以问[a,b]这个区间有多少个k的倍数时,若a>0且b>0,则ans=b/k-(a-1)/k (a-1是因为a可能是k的倍数,而b/k计算过一遍了,所以从a-1开始算,后面b+1同理),若a<0且b>0,则ans=(b+1)/k-a/k;,否则如果a<=0且b>=0则必定包含了0,所以ans=b/k-a/k+1(+1是为了加上0) 下面是测试时的一些样例
1 1 10
2 -4 4
1 -1000000000000000000 1000000000000000000
1000000000000000000 -1000000000000000000 1000000000000000000
4 -2 1
4 -3 4
4 0 1
4 -8 -3
4 -8 -4
4 -8 -5
4 5 8
4 7 8
5 1 10
**/

最新文章

  1. sprint3总结
  2. Masonry 创建Button的简单使用
  3. C语言32个关键字
  4. haproxy配置文件
  5. Ranorex入门指南
  6. w-WAITING---
  7. poj 1390 动态规划
  8. Java基础知识强化之集合框架笔记72:集合特点和数据结构总结
  9. select控件变成可输入状态
  10. UVALive 4959 Jumping monkey
  11. 温习PYTHON语法
  12. C#图像处理(3):在图像上加条形码
  13. 【转】title与alt的区别
  14. git版本控制(一)
  15. Windows Server 2012如果打开网页慢下载快的话
  16. 基于ZKWeb + Angular 4.0的开源管理后台Demo
  17. 【Alpha】Daily Scrum Meeting——Day1
  18. C#窗口传值(CSDN实例)
  19. [Noi2015]荷马史诗
  20. Oracle中rownum原理介绍

热门文章

  1. Python-添加psutil模块到python2.7版本
  2. 用Python搭建简单的HTTP服务 &middot; Zhangxu's Blog
  3. .NET Core 获取程序运行环境信息与反射的应用
  4. Git pull 卡在Unpacking objects
  5. C++走向远洋——39(指向学生类的指针)
  6. 初识Flask框架
  7. P1102 A-B 数对
  8. java反序列化-ysoserial-调试分析总结篇(4)
  9. ORACLE数据库实现主键自增
  10. React Native 在 Airbnb(译文)