codeforces483B

Friends and Presents

 CodeForces - 483B 

You have two friends. You want to present each of them several positive integers. You want to present cnt1 numbers to the first friend and cnt2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.

In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.

Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.

A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.

Input

The only line contains four positive integers cnt1cnt2xy (1 ≤ cnt1, cnt2 < 109cnt1 + cnt2 ≤ 1092 ≤ x < y ≤ 3·104) — the numbers that are described in the statement. It is guaranteed that numbers xy are prime.

Output

Print a single integer — the answer to the problem.

Examples

Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4

Note

In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.

In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.

 

sol:较为显然的,如果n满足,n+1肯定满足,即满足单调性,于是可以二分答案了

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
    ll s=0;
    bool f=0;
    char ch=' ';
    while(!isdigit(ch))
    {
        f|=(ch=='-'); ch=getchar();
    }
    while(isdigit(ch))
    {
        s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
    }
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-'); x=-x;
    }
    if(x<10)
    {
        putchar(x+'0');    return;
    }
    write(x/10);
    putchar((x%10)+'0');
    return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
ll n1,n2,X,Y;
inline bool Judge(ll n)
{
    ll a=n/X*(X-1)+n%X,b=n/Y*(Y-1)+n%Y,c=n-n/X-n/Y+n/(X*Y);
    if(a<n1||b<n2) return false;
    a-=c; b-=c;
    ll oo=max(0ll,n1-a);
    if(b+c-oo>=n2) return true;
    return false;
}
int main()
{
    ll ans;
    R(n1); R(n2); R(X); R(Y);
    ll l=1,r=(1e9+1e9)*X*Y;
    while(l<=r)
    {
        ll mid=(l+r)>>1;
        if(Judge(mid))
        {
            ans=mid; r=mid-1;
        }
        else l=mid+1;
    }
    Wl(ans);
    return 0;
}
/*
Input
3 1 2 3
Output
5

Input
1 3 2 3
Output
4

Input
3 3 2 3
output
7

Input
808351 17767 433 509
Output
826121
*/
View Code

 

posted @ 2019-04-22 12:12  yccdu  阅读(120)  评论(0编辑  收藏  举报