Marjar Cola is on sale now! In order to attract more customers, Edward, the boss of Marjar Company, decides to launch a promotion: If a customer returns x empty cola bottles or y cola bottle caps to the company, he can get a full bottle of Marjar Cola for free!
Now, Alice has a empty cola bottles and b cola bottle caps, and she wants to drink as many bottles of cola as possible. Do you know how many full bottles of Marjar Cola she can drink?
Note that a bottle of cola consists of one cola bottle and one bottle cap.
Input
There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 100), indicating the number of test cases. For each test case:
The first and only line contains four integers x, y, a, b (1 ≤ x, y, a, b ≤ 100). Their meanings are described above.
Output
For each test case, print one line containing one integer, indicating the number of bottles of cola Alice can drink. If Alice can drink an infinite number of bottles of cola, print "INF" (without the quotes) instead.
Sample Input
2 1 3 1 1 4 3 6 4
Sample Output
INF 4
Hint
For the second test case, Alice has 6 empty bottles and 4 bottle caps in hand. She can return 4 bottles and 3 caps to the company to get 2 full bottles of cola. Then she will have 4 empty bottles and 3 caps in hand. She can return them to the company again and get another 2 full bottles of cola. This time she has 2 bottles and 2 caps in hand, but they are not enough to make the exchange. So the answer is 4.
Author: WENG, Caizhi
Source: The 17th Zhejiang University Programming Contest Sponsored by TuSimple
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x, y) memset(x, y, sizeof(x))
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int casenum, casei;
int x, y, a, b;
int main()
{
scanf("%d", &casenum);
for (casei = 1; casei <= casenum; ++casei)
{
scanf("%d%d%d%d", &x, &y, &a, &b);
int ans = 0;
while (a >= x || b >= y)
{
int sum = a / x + b / y;
a -= a / x * x;
b -= b / y * y;
a += sum;
b += sum;
ans += sum;
if (ans >= 10000)break;
}
if (ans >= 10000)puts("INF");
else printf("%d\n", ans);
}
return 0;
}
/*
【题意】
一开始有a个可乐瓶和b个可乐罐
每x个可乐瓶或者y个可乐罐可以换一瓶可乐。
问你一共可以喝多少瓶可乐。
【分析】
只有在x == 1 || y == 1 || x == 2 && y == 2的时候是INF
否则每次至少会有1的损失,即下降率为4/5,答案绝对不会超过10000,其实实测只有493
于是暴力是最好的方法。
另外一种是可以用以下这一行做对INF的特判——
if (x == 1 || y == 1 || x == 2 && y == 2 && (a >= 2 || b >= 2) && a + b >= 3) { puts("INF"); continue; }
*/