A12677 | Sport Mafia
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $n$ 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 $1$ ;
- the second option is to put candies in the box. In this case, Alya will put $1$ 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 $9$ actions, the number of candies at the end will be $11$ , while Alya will eat $4$ candies in total.
You know the total number of actions $n$ and the number of candies at the end $k$ . 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 $n$ and $k$ the answer always exists.
Please note, that during an action of the first option, Alya takes out and eats exactly one candy.
For the tournament, Alya puts candies into the box, which will serve as a prize for a winner. To do that, she performs $n$ 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 $1$ ;
- the second option is to put candies in the box. In this case, Alya will put $1$ 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 $9$ actions, the number of candies at the end will be $11$ , while Alya will eat $4$ candies in total.
You know the total number of actions $n$ and the number of candies at the end $k$ . 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 $n$ and $k$ the answer always exists.
Please note, that during an action of the first option, Alya takes out and eats exactly one candy.
输入格式
The first line contains two integers $n$ and $k$ ( $1 \le n \le 10^9$ ; $0 \le k \le 10^9$ ) — the total number of moves and the number of candies in the box at the end.
It's guaranteed, that for the given $n$ and $k$ the answer exists.
It's guaranteed, that for the given $n$ and $k$ the answer exists.
输出格式
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.
输入输出样例
输入 #1
1 1
输出 #1
0
输入 #2
9 11
输出 #2
4
输入 #3
5 0
输出 #3
3
输入 #4
3 2
输出 #4
1
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 $0$ candies.
In the second example the possible sequence of Alya's actions looks as follows:
- put $1$ candy,
- put $2$ candies,
- eat a candy,
- eat a candy,
- put $3$ candies,
- eat a candy,
- put $4$ candies,
- eat a candy,
- put $5$ candies.
This way, she will make exactly $n=9$ actions and in the end the box will contain $1+2-1-1+3-1+4-1+5=11$ candies. The answer is $4$ , since she ate $4$ candies in total.
In the second example the possible sequence of Alya's actions looks as follows:
- put $1$ candy,
- put $2$ candies,
- eat a candy,
- eat a candy,
- put $3$ candies,
- eat a candy,
- put $4$ candies,
- eat a candy,
- put $5$ candies.
This way, she will make exactly $n=9$ actions and in the end the box will contain $1+2-1-1+3-1+4-1+5=11$ candies. The answer is $4$ , since she ate $4$ candies in total.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted