A16007 | Subtraction Game
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given two positive integers, $a$ and $b$ ( $a < b$ ).
For some positive integer $n$ , two players will play a game starting with a pile of $n$ stones. They take turns removing exactly $a$ or exactly $b$ stones from the pile. The player who is unable to make a move loses.
Find a positive integer $n$ such that the second player to move in this game has a winning strategy. This means that no matter what moves the first player makes, the second player can carefully choose their moves (possibly depending on the first player's moves) to ensure they win.
For some positive integer $n$ , two players will play a game starting with a pile of $n$ stones. They take turns removing exactly $a$ or exactly $b$ stones from the pile. The player who is unable to make a move loses.
Find a positive integer $n$ such that the second player to move in this game has a winning strategy. This means that no matter what moves the first player makes, the second player can carefully choose their moves (possibly depending on the first player's moves) to ensure they win.
输入格式
Each test contains multiple test cases. The first line contains the number of test cases $t$ ( $1 \le t \le 100$ ). The description of the test cases follows.
The only line of each test case contains two integers, $a$ and $b$ ( $1 \le a < b \le 100$ ).
The only line of each test case contains two integers, $a$ and $b$ ( $1 \le a < b \le 100$ ).
输出格式
For each test case, output any positive integer $n$ ( $1 \le n \le 10^6$ ) such that the second player to move wins.
It can be proven that such an $n$ always exists under the constraints of the problem.
It can be proven that such an $n$ always exists under the constraints of the problem.
输入输出样例
输入 #1
3 1 4 1 5 9 26
输出 #1
2 6 3
In the first test case, when $n = 2$ , the first player must remove $a = 1$ stone. Then, the second player can respond by removing $a = 1$ stone. The first player can no longer make a move, so the second player wins.
In the second test case, when $n = 6$ , the first player has two options:
- If they remove $b = 5$ stones, then the second player can respond by removing $a = 1$ stone. The first player can no longer make a move, so the second player wins.
- If they remove $a = 1$ stone, then the second player can respond by removing $a = 1$ stone. Afterwards, the players can only alternate removing exactly $a = 1$ stone. The second player will take the last stone and win.
Since the second player has a winning strategy no matter what the first player does, this is an acceptable output.In the third test case, the first player cannot make any moves when $n = 3$ , so the second player immediately wins.
In the second test case, when $n = 6$ , the first player has two options:
- If they remove $b = 5$ stones, then the second player can respond by removing $a = 1$ stone. The first player can no longer make a move, so the second player wins.
- If they remove $a = 1$ stone, then the second player can respond by removing $a = 1$ stone. Afterwards, the players can only alternate removing exactly $a = 1$ stone. The second player will take the last stone and win.
Since the second player has a winning strategy no matter what the first player does, this is an acceptable output.In the third test case, the first player cannot make any moves when $n = 3$ , so the second player immediately wins.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted