【2015广东工业大学新生赛G】【区间统计 左右端点计数思想】我是好人2 区间范围有多少个数 模x余y
2015-12-07 18:38
441 查看
#include<stdio.h> #include<iostream> #include<string.h> #include<string> #include<ctype.h> #include<math.h> #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 T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;} template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;} const int N=0,M=0,Z=1e9+7,ms63=1061109567; int casenum,casei; int l,r,x,y; int first(int n) { int k=n/x; return k+(k*x+y<n); } int main() { scanf("%d",&casenum); for(casei=1;casei<=casenum;++casei) { scanf("%d%d%d%d",&l,&r,&x,&y); if(l>r||x<=y)puts("0"); else { int lft=first(l); int rgt=first(r+1); printf("%d\n",rgt-lft); } } return 0; } /* 【trick&&吐槽】 对于数据的特判很有必要哦。 可能l>r,可能x<=y,都对应着非法情况需要输出0.特判掉就能AC啦! 【题意】 求区间范围在[l,r]内,有多少个数%x==y。 l,r,x,y都是[1,1e9]范围的数。 【类型】 区间统计,左右端点计数思想 【分析】 这种区间询问个数的问题,都有一种很有效的做法。 找到>=左区间的第一个数,是第一个合法的 找到>右区间的第一个数,是第一个非法的 两者下标一减就是答案。 而如果找>=n的第一个数呢? 利用倍数关系,倍数k=n/x,然后找到是k倍的满足要求的数k*x+y 然后如果k*x+y<n,说明实际的第一个数是(k+1)*x+y。 就这样找到编号就可以AC啦! 【时间复杂度&&优化】 O(T) */
相关文章推荐
- HDU 5240 Exam (好水的题)
- 北大—1006——Biorhythms
- 时间计算(heaven.pas/cpp)
- 工作依赖(job.cpp/pas)
- 题目 英雄 (BFS)
- kmp 学习 hihocoder #1015
- HDU 1096 A+B for Input-Output Practice (VIII)
- P1478
- P1035
- P1008 难度2.7
- 2020 绝对值排序
- 2021 发工资咯
- 2022 海选女主角
- 2024 C语言合法标识符
- 2025 查找最大元素
- 2026 首字母变大写
- 2027 统计元音
- 2028 Lowest Common Multiple Plus
- 2018 母牛的故事
- 2023 求平均成绩