Ran and the Lock Code Gym - 101778B

Statements

Two days ago, a woman called Fumiyo Edogawa knocked the door of Kogoro Mouri home and claimed that she is Conan's mom. Fumiyo introduced herself as Conan's mother and used fake documents to prove this. Conan, Kogoro Mouri, and his daughter Ran believed her. So, Conan went with her.

Ran was worried about Conan. After investigations, Ran discovered that Conan has been kidnapped! Ran called the police and they helped her to find Conan's location. When they arrived at that location, Ran shocked because she discovered that Conan is detained inside a locked coffin.

After examining the coffin, Ran discovered that it is locked using a modern electronic lock. Ran can open the lock only by solving T mysteries. In each mystery, Ran is giving two numbers n and a, and she needs to find the maximum number of distinct elements that can exist in a list of n positive elements with an average equals to a.

Ran knows that time is running out of her, and she must expedite in solving the mysteries to get Conan out of the coffin before his breath stopped. Ran asked you to help her in solving the mysteries. Will you leave Conan to die smothered or will you help Ran solving the mysteries?

Input

The first line contains an integer T (1 ≤ T ≤ 105), in which T is the number of mysteries.

Then T lines follow, giving the mysteries. Each line contains two integers n and a (1 ≤ n, a ≤ 109), in which n is the number of elements in the list, and a is the required average of that list.

Output

For each test case, print a single line containing the maximum number of distinct elements that can exist in a list of n positive elements with an average equals to a.

Example

Input

3
2 4
5 1
8 4

Output

2
1
7

Note

The average of a list of n elements x1, x2, ..., xn is defined as the ratio between the sum of the n elements and n. More formally:

In the first test case, Ran is asked to find the maximum number of distinct elements that can exist in a list of 2 positive elements with an average equals to 4. Ran can choose a list such as [3, 5]. So, the number of distinct elements is 2.

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
long long int M,mx,a;
int judge(long long int m)
{
    if(M*a-(4*m*a+m*m-m)/2<=(2*a-1)*(M-m)&&M*a-(4*m*a+m*m-m)/2>=M-m)
    {
        mx=max(mx,m);
        return 1;
    }
    else
    {
        return 0;
    }
}
void erfen(long long int l,long long int r)
{
    long long int m=(l+r)>>1;
    if(l>r) return ;
    else if(judge(m))
    {
        erfen(m+1,r);
    }
    else
    {
        erfen(l,m-1);
    }
}
int main()
{
    long long int t,n,x;
    scanf("%lld",&t);
    while(t--)
    {
        mx=0;
        scanf("%lld %lld",&n,&a);
        if(a==1)
        {
            printf("%d\n",1);
            continue;
        }
        x=min(n,2*a-1);
        M=n-x;
        erfen(0,M);
        printf("%lld\n",x+mx);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值