X: Rectangles
Description
如果限定矩形的边长必须为整数,且周长为定值L,那么面积在[A, B]范围内不同的矩形一共有多少个呢?
在这个问题中,当且仅当两个矩形面积不同时,视作是两个不同的矩形。
Input
输入数据的第一行包含一个整数T (1 <= T <= 10000),表示接下来一共有T组测试数据。
对于每组测试数据,包含三个整数L (1 <= L <= 2,000,000,000)、A、B (1 <= A <= B <= 250,000,000,000,000,000),含义同上。
Output
对于每组测试数据,用一行输出一个整数,代表上述问题的答案。
Sample Input
3
11 1 6
12 5 10
12 8 10
Sample Output
0
3
2
这么大的数据范围,当然是选择二分啦
就是二分找到边长的上下限,做差+1就是结果啦,因为边长只能是整数,具体的看代码体会吧
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
long long a,b,l,maxn,minn,s,n,low,high,mid;
cin>>n;
while(n--){
cin>>l>>a>>b;
maxn=0;
minn=0;
if(l&1||l<4){
cout<<0<<endl;
continue;
}else{
low=0,high=l/4;
while(high>=low){
mid=(high+low)/2;
if(mid*(l/2-mid)<a){
minn=max(minn,mid);
low=mid+1;
}else{
high=mid-1;
}
}
// cout<<low<<' '<<mid<<' '<<minn<<' ';
low=1,high=l/4;
while(high>=low){
mid=(high+low)/2;
if(mid*(l/2-mid)<=b){
maxn=max(maxn,mid);
low=mid+1;
}else{
high=mid-1;
}
}
// cout<<low<<' '<<mid<<' '<<maxn<<' ';
cout<<(maxn-minn>0?maxn-minn:0)<<endl;
}
}
return 0;
}