zoj3948——Marjar Cola

来源:互联网 发布:windows 安全配置指南 编辑:程序博客网 时间:2024/05/21 06:32
Marjar Cola

Time Limit: 1 Second      Memory Limit: 65536 KB

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 xyab (1 ≤ xyab ≤ 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

21 3 1 14 3 6 4

Sample Output

INF4

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.


规定a个空瓶或者b个瓶盖可以换一瓶酒,现在手头有n个瓶和m个瓶盖,问一共可以换多少酒?如果可以无限换,输出INF

int main()
{
    int T;
    int allBeers,x,y,a,b;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d%d",&x,&y,&a,&b);
        if((x==1||y==1)||(x==2&&y==2&&a==2&&b==2)){printf("INF\n");continue;}
        int beers=a/x+b/y;
        int tmp=beers;
        allBeers=beers;
        int bols=a%x,caps=b%y;
        bool f=0;
        while (beers!=0) {
            bols += beers;
            caps += beers ;
            if(a==bols&&caps==b){f=1;break;}
            beers = 0;
            allBeers += bols /x;
            beers += bols /x;
            bols -= (bols /x) * x;
            allBeers += caps /y;
            beers += caps /y;
            caps -= (caps /y) * y;
        }
        if(!f)
        printf("%d\n",allBeers);
        else printf("INF\n");
    }
    return 0;
}

1 0
原创粉丝点击