Time limit : 2sec / Memory limit : 256MB
Score : 700 points
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
- 1≤Q≤100
- 1≤Ai,Bi≤109(1≤i≤Q)
- 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
8 1 4 10 5 3 3 4 11 8 9 22 40 8 36 314159265 358979323
Sample Output 1
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.
题意:分数等于A,B两次竞赛的乘积,求分数小于Takahashi的最大可能人数。
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
int t;
long long a,b,pro;
long long sq,res=0;
cin>>t;
while (t--) {
cin>>a>>b;
pro=a*b;//乘积
sq=sqrt(pro);//所有可能中,每对数必含从1到sq其中一个数
res=sq*2;//总可能,A,B两组,所以乘以2。
if(sq*sq==pro)//题目要求小于,等于的不行,res中sq*sq看做两组,所以去掉两组。
{
res=res-2;
}
else if((pro-1)/sq==sq)//sq*sq小于pro,但是最后一组乘积是sq*sq的话,需要去掉一组
{
res--;
}
if(!(a==b&&b==sq))//a,b不是sq的话,就还没有被去掉,这里去掉。
{
res--;
}
cout<<res<<endl;
}
return 0;
}