• 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('
    ')
    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
  • 相关阅读:
    [原创]软件性能测试培训
    100w条记录分页,可以有多快?—— DataReader分页与SQL语句分页的对比测试(在线演示)
    【自然框架】注册会员活动——第一份代码的修改建议(第一版)
    加班有几种情况?兼谈讨论的方式。
    衔着树枝飞跃太平洋的傻鸟!(童话版)
    参加活动的好处。
    【自然框架】开源社区活动,会员注册的第一份代码!
    【自然框架】数据访问之精雕细琢(一)存储过程的参数
    自然框架开发系列(一):自然框架 和 AgileEAS.NET 合作,开发b/s的药店系统!
    自然框架开源社区的第一次活动——实现会员注册
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10749401.html
Copyright © 2020-2023  润新知