Codeforces Round #275 (Div. 2) B. Friends and Presents 二分+数学

8493833                 2014-10-31 08:41:26     njczy2010                     B - Friends and Presents                          GNU C++     Accepted 31 ms 4 KB

 

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 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.

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 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.

 

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<map>
  9 #include<set>
 10 #include<string>
 11 //#include<pair>
 12 
 13 #define N 3000
 14 #define M 1005
 15 #define mod 1000000007
 16 //#define p 10000007
 17 #define mod2 100000000
 18 #define ll long long
 19 #define LL long long
 20 #define maxi(a,b) (a)>(b)? (a) : (b)
 21 #define mini(a,b) (a)<(b)? (a) : (b)
 22 
 23 using namespace std;
 24 
 25 ll cnt1,cnt2,x,y;
 26 ll c;
 27 ll tot;
 28 
 29 void ini()
 30 {
 31     tot=cnt1+cnt2;
 32 }
 33 
 34 ll gcd(ll a,ll b)
 35 {
 36     if(b==0){
 37         return a;
 38     }
 39     else
 40         return gcd(b,a%b);
 41 }
 42 
 43 ll lcm(ll a,ll b)
 44 {
 45     return a/gcd(a,b)*b;
 46 }
 47 
 48 ll ok(ll m)
 49 {
 50     ll c1,c2,c3;
 51     c1=m-m/x;
 52     c2=m-m/y;
 53     c3=m-m/x-m/y+m/c;
 54     //printf("   c1=%I64d c2=%I64d c3=%I64d c=%I64d m=%I64d\n",c1,c2,c3,c,m);
 55     if(c1>=cnt1 && c2>=cnt2 && c1+c2-c3>=tot){
 56         return 1;
 57     }
 58     else{
 59         return 0;
 60     }
 61 }
 62 
 63 void solve()
 64 {
 65     c=lcm(x,y);
 66    // printf("  c=%I64d\n",c);
 67     ll l,r,m;
 68     l=1,r=20000000009;
 69     m=(l+r)/2;
 70     while(l<r)
 71     {
 72       //  printf(" l=%I64d r=%I64d m=%I64d\n",l,r,m);
 73         if(ok(m)==1){
 74             r=m;
 75         }
 76         else{
 77             l=m+1;
 78         }
 79         m=(l+r)/2;
 80     }
 81    // printf(" l=%I64d r=%I64d m=%I64d\n",l,r,m);
 82     printf("%I64d\n",m);
 83 }
 84 
 85 void out()
 86 {
 87 
 88 }
 89 
 90 int main()
 91 {
 92     //freopen("data.in","r",stdin);
 93     //freopen("data.out","w",stdout);
 94    // scanf("%d",&T);
 95    // for(int ccnt=1;ccnt<=T;ccnt++)
 96    // while(T--)
 97     while(scanf("%I64d%I64d%I64d%I64d",&cnt1,&cnt2,&x,&y)!=EOF)
 98     {
 99        // if(n==0 && m==0 ) break;
100         //printf("Case %d: ",ccnt);
101         ini();
102         solve();
103         out();
104     }
105 
106     return 0;
107 }

 

posted on 2014-10-31 13:46  njczy2010  阅读(314)  评论(0编辑  收藏  举报