Educational Codeforces Round 117 (Rated for Div. 2)C. Chat Ban

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.
二分查找
首先根据等差数列前n项和,求最多能发几条短信,然后利用二分找信息数

#include <bits/stdc++.h>
#define ll long long
//dfs 大法师
using namespace std;
const int mod=20100403;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
ll k,x;
ll s(ll xx)
{
    if(xx<=k)return xx*(xx+1)/2;
    else return k*(k+1)/2+(xx-k)*(2*k-(xx-k)-1)/2;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>k>>x;
        ll l=1,r=2*k-1;
        while(l<r)
        {
            ll mid=(l+r)/2;
            if(s(mid)<x)
            {
                l=mid+1;
            }
            else r=mid;
        }
        cout<<r<<endl;

    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值