【CodeForces】597A - Divisibility(容斥原理,数学)

来源:互联网 发布:专业淘宝图片拍摄价格 编辑:程序博客网 时间:2024/05/29 19:51

点击打开题目

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.

Examples
input
1 1 10
output
10
input
2 -4 4
output
5


分三种情况:

①a、b都大于0

②a、b都小于0

③a小于0,b大于0

(等于0的情况在①②中包含)

分别求出a、b到0可以整除的数,相减后再判断 “ 小数 ” 是否可以整除k,可以的话要+1。


代码如下:

#include <cstdio>int main(){__int64 k,a,b;__int64 ans;scanf ("%I64d %I64d %I64d",&k,&a,&b);if (a >= 0){ans = b / k - a / k;if (a % k == 0)ans++;}else if (b <= 0){a = -a;b = -b;ans = a / k - b / k;if (b % k == 0)ans++;}elseans = (-a) / k + b / k + 1;printf ("%I64d\n",ans);return 0;}


0 0
原创粉丝点击