您的位置:首页 > 其它

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

2016-06-14 17:43 323 查看
点击打开题目

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 k, a 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++;
}
else
ans = (-a) / k + b / k + 1;
printf ("%I64d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: