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.
The only line contains four positive integers cnt1, cnt2, x, y (1 ≤ cnt1, cnt2 < 109; cnt1 + cnt2 ≤ 109; 2 ≤ x < y ≤ 3·104) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
Print a single integer — the answer to the problem.
3 1 2 3
5
1 3 2 3
4
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.
#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;
}