CodeForces 483B Friends and Presents

 Friends and Presents
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 Input

Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 typedef long long LL;
 9 LL cnt1, cnt2, x, y;
10 
11 bool check(LL v)
12 {
13     LL f1,f2,both,others,ff1,ff2,gf1,gf2;
14      f1 = v / x;
15      f2 = v / y;
16      both = v / (x*y);
17      others = v - f1 - f2 + both;
18      ff1 = f1 - both;  
19      ff2 = f2 - both;  
20 
21      gf1 = (cnt1 - ff2 >= 0 ? cnt1 - ff2 : 0);
22      gf2 = (cnt2 - ff1 >= 0 ? cnt2 - ff1 : 0);
23 
24     return (gf1 + gf2 <= others);
25 }
26 
27 int main()
28 {
29     while (scanf("%I64d%I64d%I64d%I64d", &cnt1, &cnt2, &x, &y) != EOF)
30     {
31         LL l=1, r=1e18;
32         while (l<r)
33         {
34             LL m=(l+r)/2;
35             if (check(m))
36                 r=m;
37             else
38                 l=m+1;
39         }
40         printf("%I64d
",r);
41     }
42     return 0;
43 }
View Code
原文地址:https://www.cnblogs.com/cyd308/p/4771543.html