#abc269g. G - Reversible Cards 2
G - Reversible Cards 2
Score : points
问题描述
我们有 张卡片,编号为 到 。
第 张卡片正面写有整数 ,背面写有整数 。这里,。
对于每个 ,解决以下问题:
这 张卡片被排列成正面朝上可见的状态。你可以选择翻转从 到 张(包含)任意数量的卡片。
为了使得所有可见数字之和等于 ,至少需要翻转多少张卡片?请输出这个卡片的数量。
如果无法通过翻转卡片使得可见数字之和等于 ,则输出 。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
We have cards numbered to .
Card has an integer written on the front and an integer written on the back. Here, .
For each , solve the following problem.
The cards are arranged so that their front sides are visible. You may choose between and cards (inclusive) and flip them.
To make the sum of the visible numbers equal to , at least how many cards must be flipped? Print this number of cards.
If there is no way to flip cards to make the sum of the visible numbers equal to , print instead.
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print lines. The -th line should contain the answer for .
Sample Input 1
3 6
0 2
1 0
0 3
Sample Output 1
1
0
2
1
1
3
2
For , for instance, flipping just card makes the sum of the visible numbers . This choice is optimal.
For , flipping all cards makes the sum of the visible numbers . This choice is optimal.
Sample Input 2
2 3
1 1
0 1
Sample Output 2
-1
0
1
-1
Sample Input 3
5 12
0 1
0 3
1 0
0 5
0 2
Sample Output 3
1
0
1
1
1
2
1
2
2
2
3
3
4
update @ 2024/3/10 11:22:14