【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
output
input
output
分三种情况:
①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;
}
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;
}
相关文章推荐
- 第一节课作业150206309
- 连接池
- 欧几里得距离转换(EDT)算法
- 删除宽字符串
- ANDROID 混淆
- 监听EditText输入框 ,判断输入的密码是什么格式
- 原生js封装ajax 案例
- chaper3_exerise_Uva1568_Molar_Mass_分子量
- 梦断代码阅读笔记一
- 转:mysql中int、bigint、smallint 和 tinyint的区别与长度的含义
- 详解依赖注入(DI)和Ioc容器
- javascript闭包
- Android BLE 中心和外设通信时 中心设备的onCharacteristicChanged() 没有回调
- D3.js学习02_数据绑定data
- Swift和OC,是编译型语言、解释性语言、运行时语言?
- Spring-Task 定时任务调度
- iOS 如何在一个存在多个project的workspace中引入cocoapods管理第三方类库
- Linux用户和权限管理
- springmvc 获取request response
- c语言字符数组与字符串的使用详解