luogu#P14974. [USACO26JAN1] Chip Exchange B
[USACO26JAN1] Chip Exchange B
Problem Description
Bessie the cow has in her possession chips of type A and chips of type B (). She can perform the following operation as many times as she likes:
- If you have at least chips of type B, exchange chips of type B for chips of type A ().
Determine the minimum non-negative integer such that the following holds: after receiving additional random chips, it is guaranteed that Bessie can end up with at least chips of type A ().
Input Format
The first line contains , the number of independent test cases ().
Then follow tests, each consisting of five integers .
Output Format
Output the answer for each test on a separate line.
Note: The large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).
2
2 3 1 1 6
2 3 1 1 4
1
0
5
0 0 2 3 5
0 1 2 3 5
1 0 2 3 5
10 10 2 3 5
0 0 1 1000000000 1000000000
9
8
7
0
1000000000000000000
Hint
For the first test, Bessie initially starts with no chips. If she receives any additional chips, she can perform the operation to end up with at least chips of type A. For example, if she receives chips of type A and chips of type B, she can perform the operation twice to end up with chips of type A. However, if she only receive chips of type B, she can only end up with chips of type A.
For the fourth test, she already has enough chips of type A from the start.
- Input 3:
- Inputs 4-5: for all cases
- Inputs 6-7: ,
- Inputs 8-12: No additional constraints.