AtCoder Beginner Contest 093 D - Worst Case (寻找(x*y)小于(a*b)的最大组数)

Problem Statement

101010 participants, including Takahashi, competed in two programming contests. In each contest, all participants had distinct ranks from first through 101010-th.

The score of a participant is the product of his/her ranks in the two contests.

Process the following Q queries:

  • In the i-th query, you are given two positive integers Ai and Bi. Assuming that Takahashi was ranked Ai-th in the first contest and Bi-th in the second contest, find the maximum possible number of participants whose scores are smaller than Takahashi's.

Constraints

  • 1Q100
  • 1Ai,Bi109(1iQ)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
A1 B1
:
AQ BQ

Output

For each query, print the maximum possible number of participants whose scores are smaller than Takahashi's.


Sample Input 1

Copy
8
1 4
10 5
3 3
4 11
8 9
22 40
8 36
314159265 358979323

Sample Output 1

Copy
1
12
4
11
14
57
31
671644785

Let us denote a participant who was ranked x-th in the first contest and y-th in the second contest as (x,y).

In the first query, (2,1) is a possible candidate of a participant whose score is smaller than Takahashi's. There are never two or more participants whose scores are smaller than Takahashi's, so we should print 1

题意:q次查询,给出a,b,求最多的可能组数使得 x*y

思路:组数与sqrt(a*b)有关,然后根据关系来计算,可以当一个结论记住

#include
using namespace std;
typedef long long ll;
 
int main()
{
	int n;
	cin >> n;
	ll a, b, pr;
	ll ans;
	for(int i = 1;i <= n; i++)
	{
		cin >> a >> b;
		pr = a * b;
		ll t = sqrt(pr);
		if(t * (t + 1) < pr)
			ans = t * 2 - 1;
		else if(t * t == pr)
		{
			if(a == b)
				ans = (t - 1) * 2;
			else
				ans = (t - 1) * 2 - 1;
		}
		else
			ans = (t - 1) * 2;
		cout << ans << endl;
	}
	return 0;
}


你可能感兴趣的:(Atcoder,ACM_数字处理与数论)