您的位置:首页 > 其它

【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)

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  水题