AtCoder Regular Contest 094 D - Worst Case(数学思维)

D - Worst Case


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

  • 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.


BUG反馈门

题意:Q组询问 a b 求解 x*y<a*b 中x y的组数 且x y在对应的位置不重复出现 

example: 

3 3

ans : 
2 4
4 2
5 1
1 5

意思就是说 左右列不能存在重复

分析:

由于需要得到最大的组数且不存在重复的情况 因此:
get_中间值mid=sqrt(a*b) 
一边增加(l++) 一边递减(r--)  必然能使得 l*r<x*y,证明略...
由于两边存在一定的对称结构 因此直接判断一下中间值附近对ans的影响就好了 
该题也可作为结论来记住

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main (){
    ios_base::sync_with_stdio(false);
    int t;cin>>t;
    while(t--){
        ll a,b,ans=0;cin>>a>>b;
        if(a==b) ans=2*a-2;
        else {
            ll tmp=sqrt(a*b);
            if(tmp*tmp==a*b) tmp--;
            if(tmp*(tmp+1)>=a*b) ans=2*tmp-2;
            else ans=2*tmp-1;
        }
        cout<<ans<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值