CF_483B Friends and Presents(二分)

B. Friends and Presents
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Sample test(s)
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 135 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.

题意:
你要给两个小朋友分礼物,礼物就是从1到V的数字,第一个要cnt1个但是不要能被x整除的,第二个要cnt2个但不要能被y整除的。目的求最小的V使能满足两个小朋友的要求。x,y都是素数,碰到两个数都能整除的不给就是了。
题解:
找最小的能满足的数,正着想不好想,得先把V整出来这样也好讨论情况。但是从小到大逐个遍历不现实,但因为没个数的判断条件很简单,所以用二分来找这个数是比较方便的。确定好大的框架后,注意点就放在怎么判断能否满足条件了。1~V中只有四种类型的数,能被x整除的,能被y整除的,能被x,y同时整除的,x,y都不能整除的,搞清楚他们各自的数量判断起来就很简单了。
代码实现:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <cmath>
#define INF pow(10,10)
#define min(a,b) a<b?a:b
#define max(a,b) a>b?a:b

using namespace std;
long long result;
long long a1,a2,a3,mid;
bool if_ok(long long);
int cnt1,cnt2,x,y;
int main()
{
    scanf("%d%d%d%d",&cnt1,&cnt2,&x,&y);
    double lb=1,ub=INF;
    result=INF;
    for(int i=0;i<100;i++)
    {
        /** \关于此处相邻点的处理
         *
         * \对于题目要求仅仅是整数来说的题目来说其实差别不大但是牵涉到极限情况左右边界能否取到的问题。同样是强制转换,
         * \但是+0.5就是右边界有限,不加就是左边界优先,整形强制转换默认是直接舍弃小数点的。不同的题目对于两边边界的需
         * \求是不一样的,也就是说看那边边界是对答案有影响的。当然这也和自己本身的初始化有关。对于这道题来说我左界为1,
         * \右界是比较大的数,相比来说1是不会取到的,所以我处理为右边界优先,对于这些情况我觉得还是要仔细讨论
         *
         */
        mid=(long long)((lb+ub)/2+0.5);
        if(if_ok(mid))  ub=mid;
        else lb=mid;
        if(lb==ub||mid-lb==1)
            break;
    }
    printf("%I64d\n",result);
    return 0;
}
//判断函数
bool if_ok(long long num)
{
    a1=num-num/x;//能分给1的数
    a2=num-num/y;//能分给2的数
    a3=num-num/x-num/y+num/(x*y);//1,2共可以享的数
    if(a1-a3>=cnt1)//如果1自己的就够用
    {
        if(a2>=cnt2)//2随便用也够用
        {
            result=min(num,result);
            return true;
        }
    }
    else//1得用掉一部分1,2共享的数
    {
        if(a1>=cnt1)//1全用完够
        {
            if(a1+a2-a3-cnt1-cnt2>=0)//2只用自己的也够
            {
                result=min(num,result);
                return true;
            }
        }
    }
    return false;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值