您的位置:首页 > 其它

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

2014-10-31 13:46 513 查看
8493833 2014-10-31 08:41:26 njczy2010 B - Friends and Presents GNU C++ Accepted31 ms4 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.

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<string>
//#include<pair>

#define N 3000
#define M 1005
#define mod 1000000007
//#define p 10000007
#define mod2 100000000
#define ll long long
#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b)

using namespace std;

ll cnt1,cnt2,x,y;
ll c;
ll tot;

void ini()
{
tot=cnt1+cnt2;
}

ll gcd(ll a,ll b)
{
if(b==0){
return a;
}
else
return gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
return a/gcd(a,b)*b;
}

ll ok(ll m)
{
ll c1,c2,c3;
c1=m-m/x;
c2=m-m/y;
c3=m-m/x-m/y+m/c;
//printf("   c1=%I64d c2=%I64d c3=%I64d c=%I64d m=%I64d\n",c1,c2,c3,c,m);
if(c1>=cnt1 && c2>=cnt2 && c1+c2-c3>=tot){
return 1;
}
else{
return 0;
}
}

void solve()
{
c=lcm(x,y);
// printf("  c=%I64d\n",c);
ll l,r,m;
l=1,r=20000000009;
m=(l+r)/2;
while(l<r)
{
//  printf(" l=%I64d r=%I64d m=%I64d\n",l,r,m);
if(ok(m)==1){
r=m;
}
else{
l=m+1;
}
m=(l+r)/2;
}
// printf(" l=%I64d r=%I64d m=%I64d\n",l,r,m);
printf("%I64d\n",m);
}

void out()
{

}

int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
// scanf("%d",&T);
// for(int ccnt=1;ccnt<=T;ccnt++)
// while(T--)
while(scanf("%I64d%I64d%I64d%I64d",&cnt1,&cnt2,&x,&y)!=EOF)
{
// if(n==0 && m==0 ) break;
//printf("Case %d: ",ccnt);
ini();
solve();
out();
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: