Polycarp has xx of red and yy of blue candies. Using them, he wants to make gift sets. Each gift set contains either aa red candies and bb blue candies, or aa blue candies and bb red candies. Any candy can belong to at most one gift set.
Help Polycarp to find the largest number of gift sets he can create.
For example, if x=10x=10, y=12y=12, a=5a=5, and b=2b=2, then Polycarp can make three gift sets:
- In the first set there will be 55 red candies and 22 blue candies;
- In the second set there will be 55 blue candies and 22 red candies;
- In the third set will be 55 blue candies and 22 red candies.
Note that in this example there is one red candy that Polycarp does not use in any gift set.
Input
The first line contains an integer tt (1≤t≤1041≤t≤104). Then tt test cases follow.
Each test case consists of a single string containing four integers xx, yy, aa, and bb (1≤x,y,a,b≤1091≤x,y,a,b≤109).
Output
For each test case, output one number — the maximum number of gift sets that Polycarp can make.
Example
Input
9 10 12 2 5 1 1 2 2 52 311 13 27 1000000000 1000000000 1 1 1000000000 1 1 1000000000 1 1000000000 1000000000 1 1 2 1 1 7 8 1 2 4 1 2 3
Output
3 0 4 1000000000 1 1 1 5 0

#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int t,x,y,a,b;
bool check(int mid)
{
if(b==a)
return mid<=min(x/a,y/b);
int l=(int)ceil(1.0*(y-a*mid)/(b-a));
int r=(int)floor(1.0*(x-b*mid)/(a-b));
l=max(0LL,l),r=min(r,mid);
if(l<=r) return 1;
else return 0;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>t;
while(t--)
{
cin>>x>>y>>a>>b;
if(a<b) swap(a,b);
int l=0,r=max(x/a,y/b),ans=0;
while(l<=r)
{
int mid=l+r>>1;
if(check(mid)) l=mid+1,ans=mid;
else r=mid-1;
}
cout<<ans<<endl;
}
return 0;
}

给定红蓝两种糖果的数量及每种组合的分配方式,求能组成的最大礼物套装数。例如,当红糖果10个,蓝糖果12个,每组需要5个红和2个蓝时,可以组成3个礼物套装。程序通过动态规划找到最大值。

340

被折叠的 条评论
为什么被折叠?



