You are a usual chat user on the most famous streaming platform. Of course, there are some moments when you just want to chill and spam something.
More precisely, you want to spam the emote triangle of size k. It consists of 2k−1 messages. The first message consists of one emote, the second one — of two emotes, ..., the k-th one — of k emotes, the k+1-th one — of k−1 emotes, ..., and the last one — of one emote.
For example, the emote triangle for k=3 consists of 5 messages:
Of course, most of the channels have auto moderation. Auto moderator of the current chat will ban you right after you spam at least x emotes in succession (you can assume you are the only user in the chat). Now you are interested — how many messages will you write before getting banned? Or maybe you will not get banned at all (i.e. will write all 2k−1 messages and complete your emote triangle successfully)? Note that if you get banned as a result of writing a message, this message is also counted.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases. The next t lines describe test cases.
The only line of the test case contains integers k and x (1≤k≤109;1≤x≤1018).
Output
For each test case, print the number of messages you will write before getting banned for the corresponding values k and x.
Example
input
Copy
7 4 6 4 7 1 2 3 7 2 5 100 1 1000000000 923456789987654321
output
Copy
3 4 1 4 3 1 1608737403
Note
Let's analyze the test cases of the example.
- In the first test case, you write three messages containing 1, 2 and 3 emotes respectively, and since 1+2+3≥6, you get banned after that.
- In the second test case, you write four messages containing 1, 2, 3 and 4 emotes respectively, and since 1+2+3+4≥7, you get banned after that.
- In the third test case, you write one message containing exactly 1 emote. It doesn't get you banned, since 1<2, but you have already finished posting your emote triangle. So you wrote one message successfully.
- In the fourth test case, you write four messages containing 1, 2, 3 and 2 emotes respectively, and since 1+2+3+2≥7, you get banned after that.
- In the fifth test case, you write three messages containing 1, 2 and 1 emote respectively. It doesn't get you banned, since 1+2+1<5, but you have already finished posting your emote triangle. So you wrote three messages successfully.
- In the sixth test case, since x=1, you get banned as soon as you send your first message.
- The seventh test case is too large to analyze, so we'll skip it.
思路:分为前缀和,然后再二分查找。
前缀和:这个题是前缀和,然后找和的二分(和是前缀和),消息是先增加,后减少,前缀和的公式为:k*(k+1)/2(这是前半部分的求和公式),后半部分很明显无法没法使用这个公式(不等差了).那么后半部分的和怎么算呢?这里我们可以用全部的减去剩下的(没有取到或用到的)就能得到大于k时的前缀了。然后我们二分找一下。
二分:二分其实不难,当x小于等于mid时(mid是中间的那个数).我们可以先将此时的位置记录下来,将R推到mid-1,为什么不推到mid呢?(此时,mid要么在x上,要么x在前面,记录下此时位置可以很好地处理x与mid重合,那么只剩x小于mid的情况了,mid再往前推一个也无妨)这里一定要切记当R变化时在记录答案(位置),在L发生变化时记录容易产生冲突! 这里的结束条件是L<=R。L的变化也是如此,L=mid+1.二分就是我们想要的数,在数组(一串数字,一维数字)中,位于“中位数”(即mid)的左边还是右边(大于小于)。二分也能无限逼近某个数(精确数值)(找位置多加个变量)
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
int main()
{
int t;
cin>>t;
while(t--)
{
ll k,x;
cin>>k>>x;
if(k*k<=x)
{
cout<<2*k-1<<endl;
}
else
{
ll l=0,r=2*k-1,ans=2*k-1;
while(l<=r)
{
ll mid=(l+r)/2;
ll st=0;//当前个数
if(mid<=k) st=mid*(mid+1)/2;
else
{
ll p=2*k-1-mid;
st=k*k-p*(p+1)/2;
}
if(st>=x) ans=mid,r=mid-1;
else l=mid+1;
}
cout<<ans<<endl;
}
}
return 0;
}