【Codeforces Testing Round 12A】【讨论 边界元素映射】Divisibility 区间范围内k倍数的数的个数
2015-11-27 09:23
573 查看
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.
Sample test(s)
input
output
input
output
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T> inline void gmax(T &a,T b){if(b>a)a=b;}
template <class T> inline void gmin(T &a,T b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
LL a,b,k;
int main()
{
while(~scanf("%lld%lld%lld",&k,&a,&b))
{
LL lft=a/k;if(a>0&&a%k)++lft;
LL rgt=b/k;if(b<0&&b%k)--rgt;
printf("%lld\n",rgt-lft+1);
}
return 0;
}
/*
【题意】
给你一个模数k(1<=k<=1e18),
然后再给你一个区间[a,b](-1e18<=a<=b<=1e18),
问你这个区间内有多少个数,使得这个数为k的倍数。
【类型】
讨论
【分析】
第一个想法是,我们求出区间长度a-b+1,然后看这个区间长度是k的几倍。
然而,问题很显然。比如说k=5,我们一个长度为6的区间内,k倍数的数可能是1个或2个。
对于区间[0,5],k倍数的数为2个;对于区间[1,6],k倍数的数只有1个。
于是我们发现,我们还需要特殊判定,处于边界位置的k倍数的数。
既然如此,我们不妨直接映射到边界位置的k倍数的数——
LL lft=a/k;if(a>0&&a%k)++lft;//最左的第一个数是lft
LL rgt=b/k;if(b<0&&b%k)--rgt;//最右的第一个数是rgt
printf("%lld\n",rgt-lft+1);//答案就是rgt-lft+1
做完喽!边界映射的思想棒棒哒!
*/
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.
Sample test(s)
input
1 1 10
output
10
input
2 -4 4
output
5
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T> inline void gmax(T &a,T b){if(b>a)a=b;}
template <class T> inline void gmin(T &a,T b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
LL a,b,k;
int main()
{
while(~scanf("%lld%lld%lld",&k,&a,&b))
{
LL lft=a/k;if(a>0&&a%k)++lft;
LL rgt=b/k;if(b<0&&b%k)--rgt;
printf("%lld\n",rgt-lft+1);
}
return 0;
}
/*
【题意】
给你一个模数k(1<=k<=1e18),
然后再给你一个区间[a,b](-1e18<=a<=b<=1e18),
问你这个区间内有多少个数,使得这个数为k的倍数。
【类型】
讨论
【分析】
第一个想法是,我们求出区间长度a-b+1,然后看这个区间长度是k的几倍。
然而,问题很显然。比如说k=5,我们一个长度为6的区间内,k倍数的数可能是1个或2个。
对于区间[0,5],k倍数的数为2个;对于区间[1,6],k倍数的数只有1个。
于是我们发现,我们还需要特殊判定,处于边界位置的k倍数的数。
既然如此,我们不妨直接映射到边界位置的k倍数的数——
LL lft=a/k;if(a>0&&a%k)++lft;//最左的第一个数是lft
LL rgt=b/k;if(b<0&&b%k)--rgt;//最右的第一个数是rgt
printf("%lld\n",rgt-lft+1);//答案就是rgt-lft+1
做完喽!边界映射的思想棒棒哒!
*/
相关文章推荐
- this[] 指的是什么内容 讨论
- 对JavaScript的eval()中使用函数的进一步讨论
- 有个同学说:程序员很苦逼,而我,觉得很有意思!
- 【2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest D】【模拟 讨论】Boulevard 人流行走看是否相交 beautiful code
- 【Codeforces Round 324 (Div 2)C】【构造 讨论】Marina and Vasya 构造c串使得c串与a串b串各有m个字符不同
- 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest A】【水题 讨论】Alex Origami Squares 矩形内置三个最大正方形
- 【Educational Codeforces Round 1A】【水题】Tricky Sum 1~n之和减去2的幂
- 【Educational Codeforces Round 1B】【字符串平移 水题】Queries on a String 字符串平移水题
- 【Educational Codeforces Round 1C】【计算几何-极角排序 atan2 long double】Nearest vectors 平面图上原点引出角度最小的两个
- 【Educational Codeforces Round 1D】【DFS 联通块打标记法】Igor In the Museum 联通块内墙的面数
- 【Educational Codeforces Round 1E】【动态规划-多维DP】Chocolate Bar 矩形巧克力掰开吃的最小成本
- 【Codeforces Round 299 (Div 2)D】【KMP 本质是最前与最后匹配】Tavas and Malekas 长度为n的匹配串被模板串多位点覆盖的匹配串个数
- 【Codeforces Round 333 (Div 2)A 】【水题 简单进制模拟】Two Bases 不同进制数比大小
- 【Codeforces Round 333 (Div 2)B】【贪心 多指针】Approximating a Constant Range 给定数组 相邻元素波动为1 求差值不超1的最长序连续子序列
- 【Codeforces Round 333 (Div 2)C】【最短路】The Two Routes 完全图两种双向边的最小最大距离
- 【Codeforces Round 333 (Div 2)D】【线段树 or ST-RMQ 初始化78msAC】Lipshitz Sequence 若干区间询问所有子区间的答案和
- 【Codeforces Round 333 (Div 2)E】【期望DP概率做法 树状数组转前缀和】Kleofáš and the n-thlon n场比赛m个人获得总名次的期望
- 2048游戏分析、讨论与扩展 - Part I - 游戏分析与讨论
- 讨论76 怎么查一下我机器的内存?AIX环境
- 讨论Java直播课程