1204: Rectangles
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 770 Solved: 112
[ Submit][ Status][ Web Board]
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
311 1 612 5 1012 8 10
Sample Output
032
二分找出面积小于a,b的最大边,作差即可。
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
typedef long long ll;
ll l, a, b;
int main(int argc, char const *argv[])
{
int t;
scanf("%d", &t);
while(t--) {
scanf("%lld%lld%lld", &l, &a, &b);
if(l % 2) {
printf("0\n");
continue;
}
ll c = l >> 1, left = 1, right = c >> 1, num1 = 0, num2 = 0;
while(left <= right) {
ll mid = (left + right) >> 1;
if(mid * (c - mid) < a) {
num1 = max(num1, mid);
left = mid + 1;
}
else right = mid - 1;
}
left = 1, right = c >> 1;
while(left <= right) {
ll mid = (left + right) >> 1;
if(mid * (c - mid) <= b) {
num2 = max(num2, mid);
left = mid + 1;
}
else right = mid - 1;
}
printf("%lld\n", num2 - num1);
}
return 0;
}