time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Each evening after the dinner the SIS's students gather together to play the game of Sport Mafia.

For the tournament, Alya puts candies into the box, which will serve as a prize for a winner. To do that, she performs nn actions. The first action performed is to put a single candy into the box. For each of the remaining moves she can choose from two options:

  • the first option, in case the box contains at least one candy, is to take exactly one candy out and eat it. This way the number of candies in the box decreased by 11;
  • the second option is to put candies in the box. In this case, Alya will put 11 more candy, than she put in the previous time.

Thus, if the box is empty, then it can only use the second option.

For example, one possible sequence of Alya's actions look as follows:

  • put one candy into the box;
  • put two candies into the box;
  • eat one candy from the box;
  • eat one candy from the box;
  • put three candies into the box;
  • eat one candy from the box;
  • put four candies into the box;
  • eat one candy from the box;
  • put five candies into the box;

This way she will perform 99 actions, the number of candies at the end will be 1111, while Alya will eat 44 candies in total.

You know the total number of actions nn and the number of candies at the end kk. You need to find the total number of sweets Alya ate. That is the number of moves of the first option. It's guaranteed, that for the given nn and kk the answer always exists.

Please note, that during an action of the first option, Alya takes out and eats exactly one candy.

Input

The first line contains two integers nn and kk (1≤n≤1091≤n≤109; 0≤k≤1090≤k≤109) — the total number of moves and the number of candies in the box at the end.

It's guaranteed, that for the given nn and kk the answer exists.

Output

Print a single integer — the number of candies, which Alya ate. Please note, that in this problem there aren't multiple possible answers — the answer is unique for any input data.

Examples
input

Copy
1 1
output

Copy
0
input

Copy
9 11
output

Copy
4
input

Copy
5 0
output

Copy
3
input

Copy
3 2
output

Copy
1
Note

In the first example, Alya has made one move only. According to the statement, the first move is always putting one candy in the box. Hence Alya ate 00 candies.

In the second example the possible sequence of Alya's actions looks as follows:

  • put 11 candy,
  • put 22 candies,
  • eat a candy,
  • eat a candy,
  • put 33 candies,
  • eat a candy,
  • put 44 candies,
  • eat a candy,
  • put 55 candies.

This way, she will make exactly n=9n=9 actions and in the end the box will contain 1+2−1−1+3−1+4−1+5=111+2−1−1+3−1+4−1+5=11 candies. The answer is 44, since she ate 44 candies in total.

题意:n次操作,每次操作可以选择往盒子里放 i 个糖果(i <n),也可以在盒子里拿一个糖果吃掉,要求当n次操作之后盒子里恰好有k个糖果,问在n次操作中有几次是吃糖果

题解:二分枚举吃糖果的次数x,(0<=x<=n-1,因为第一次一定是放糖果)

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
typedef long long ll;
ll n,k;
ll find(ll x)
{
ll sum=;
for(int i=;i<=n-x;i++)
sum=sum+i;
if(sum-x>k)
return ;
else
return ;
}
int main()
{
while(cin>>n>>k)
{
ll l=,r=n-,mid;
while(l<=r)
{
mid=l+(r-l)/;
if(find(mid))
l=mid+;
else
r=mid-;
}
cout<<l<<endl;
}
return ;
}

B. Sport Mafia 二分的更多相关文章

  1. 【CF587D】Duff in Mafia 二分+前缀优化建图+2-SAT

    [CF587D]Duff in Mafia 题意:给你一张n个点m条边的无向图,边有颜色和边权.你要从中删去一些边,满足: 1.任意两条删掉的边没有公共的顶点.2.任意两条剩余的.颜色相同的边没有公共 ...

  2. Codeforces Round #574 (Div. 2)补题

    A. Drinks Choosing 统计每种酒有多少人偏爱他们. ki 为每种酒的偏爱人数. 输出ans = (n + 1)/2 >  Σki / 2 ? (n + 1)/2 - Σki / ...

  3. Codeforce Round #574(Div.2)

                                                                                                        ...

  4. Codeforces Round #574 (Div. 2)

    目录 Contest Info Solutions A. Drinks Choosing B. Sport Mafia C. Basketball Exercise D1. Submarine in ...

  5. Codeforces Round #574 (Div. 2) A~E Solution

    A. Drinks Choosing 有 $n$ 个人,每个人各有一种最喜欢的饮料,但是买饮料的时候只能同一种的两个两个买(两个一对) 学校只打算卖 $\left \lceil \frac{n}{2} ...

  6. 2020.10.16--vj个人赛补题

    D - Drinks Choosing Old timers of Summer Informatics School can remember previous camps in which eac ...

  7. CodeForces - 348A Mafia (巧妙二分)

    传送门: http://codeforces.com/problemset/problem/348/A A. Mafia time limit per test 2 seconds memory li ...

  8. Codeforces Round #202 (Div. 1) A. Mafia 推公式 + 二分答案

    http://codeforces.com/problemset/problem/348/A A. Mafia time limit per test 2 seconds memory limit p ...

  9. codeforces Mafia

    /* * Mafia.cpp * * Created on: 2013-10-12 * Author: wangzhu */ /** * 每个人都想玩若干场,求至少需要玩几场才可以满足大家的需求. * ...

随机推荐

  1. luogu P2280 激光炸弹(二维前缀和)

    由题给的xi, yi范围,可以建立二维地图maze[i][j],记录i j范围上的所有目标的价值总和 即有maze[xi][yi] += wi 然后接下来的目标就是求出该二维数组的前缀和 可得到前缀和 ...

  2. 杭电 2028 ( Lowest Common Multiple Plus )

    链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2028 题目要求:就是求最大公倍数,我百度了一下,最好实现的算法就是:       公式法 由于 ...

  3. vue + element ui table表格二次封装 常用功能

    因为在做后台管理项目的时候用到了大量的表格, 且功能大多相同,因此封装了一些常用的功能, 方便多次复用. 组件封装代码: <template> <el-table :data=&qu ...

  4. 执行SQL时出现: ORDER BY clause is not in GROUP BY clause and contains nonaggregated c

    注意: 采用navicat新建数据库时,需要将编码方式设置为,字符集:utf8 -- UTF-8 Unicode ,排序规则:utf8_general_ci 在运行sql语句时,出现以下问题: [Er ...

  5. c++ 关键字this的用法简介

    前言: 自己在程序的时候一般不用this,但是在后来发现越来越有必要好好整理一下该知识点了,如有不足之处以及缺漏之处还望各位读者指出. 概念&实例 this 是 C++ 中的一个关键字,也是一 ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:清除浮动

    <!DOCTYPE html> <html> <head> <title>Bootstrap .clearfix 实例</title> &l ...

  7. 指定GPU运行python程序

    一.命令行运行python程序时 1.首先查看哪些GPU空闲,nvidia-smi显示当前GPU使用情况. nvidia-smi 2.然后指定空闲的GPU运行python程序. CUDA_VISIBL ...

  8. java 学习地址

    数组 JAVA中数组排序小结 Java中ArrayList和LinkedList区别    在往里面插入数据时.LinkedList会比ArrayList快很多,因为前者仅仅做了一个类型的插入,而后者 ...

  9. 关闭Hyper-V后,天翼校园宽带(Netkeeper)依旧显示Sorry, this application cannot run under a Virtual Machin的解决方法

    环境: win10专业版,版本1909 经过: 尝试了一下win10 更新后的沙盒系统,当时开启了沙盒,但是未打开Hyper-V,沙盒正常运行. 第二次开机后天翼校园宽带(Netkeeper)显示So ...

  10. 拖放获取文件信息的bat代码

    参考:岁月如歌-通过拖曳获取文件信息的bat代码 拖放获取文件信息的bat代码 使用命令行配合7z解压文件时由于每次解压的文件不同,因此搜索了一下拖放识别文件信息的方法,以此方式来减轻工作量 获取文件 ...