Friends and Presents

CodeForces - 483B

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 ≤ 109; 2 ≤ 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.

Examples

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.

sol:较为显然的,如果n满足,n+1肯定满足,即满足单调性,于是可以二分答案了

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
ll n1,n2,X,Y;
inline bool Judge(ll n)
{
ll a=n/X*(X-)+n%X,b=n/Y*(Y-)+n%Y,c=n-n/X-n/Y+n/(X*Y);
if(a<n1||b<n2) return false;
a-=c; b-=c;
ll oo=max(0ll,n1-a);
if(b+c-oo>=n2) return true;
return false;
}
int main()
{
ll ans;
R(n1); R(n2); R(X); R(Y);
ll l=,r=(1e9+1e9)*X*Y;
while(l<=r)
{
ll mid=(l+r)>>;
if(Judge(mid))
{
ans=mid; r=mid-;
}
else l=mid+;
}
Wl(ans);
return ;
}
/*
Input
3 1 2 3
Output
5 Input
1 3 2 3
Output
4 Input
3 3 2 3
output
7 Input
808351 17767 433 509
Output
826121
*/

codeforces483B的更多相关文章

  1. Codeforces483B. Friends and Presents(二分+容斥原理)

    题目链接:传送门 题目: B. Friends and Presents time limit per test second memory limit per test megabytes inpu ...

随机推荐

  1. WLST

    Master Note on WebLogic Server Scripting Tool (WLST) Usage, Sample Scripts and Known Issues Deployin ...

  2. 互联网安全中心(CIS)发布新版20大安全控制

    这些最佳实践最初由SANS研究所提出,名为“SANS关键控制”,是各类公司企业不可或缺的安全控制措施.通过采纳这些控制方法,公司企业可防止绝大部分的网络攻击. 有效网络防御的20条关键安全控制 对上一 ...

  3. Linux 安装 powershell

    linux 安装 powershell Intro powershell 已经推出了一个 Powershell Core, 版本号对应 Powershell 6.x,可以跨平台,支持 Linux 和 ...

  4. Docker创建JIRA 7.2.4中文破解版

    目录 目录 1.介绍 1.1.什么是JIRA? 2.JIRA的官网在哪里? 3.如何下载安装? 4.对JIRA进行配置 4.1.打开浏览器:http://localhost:20012 4.2.JIR ...

  5. .net 支付宝接口小小误区

    1.该密匙目测不是私钥,应用官方文档生成的长私钥. 2. 此公钥用的是应用公钥 3.设置支付完成后的通知页面和回调页面 其他的按照官方文档的demo来实现即可

  6. 使用django 中间件在所有请求前执行功能

    django中间是一个轻级,低耦合的插件,用来改变全局的输入和输出. 一 如何使用中间件 定义中间件 注册中间件 # 这是一个中间件代码片段的说明,在各个位置的代码将在何时执行 def simple_ ...

  7. selenium-获取元素属性(六)

    获取元素属性很简单,使用 get_attribute 方法即可 如下图 获取具体的属性直接将该属性名当作参数传入即可 若是获取值,则获取的实则是该元素的 value,需要将 value 当参数传入 i ...

  8. git执行cherry-pick时修改提交信息

    git执行cherry-pick时修改提交信息 在本地分支执行cherry-pick命令时有时需要修改commit message信息,可以加参数-e实现: git cherry-pick -e co ...

  9. Angular安装及创建第一个项目

    Angular简介 AngularJS 诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJ ...

  10. Django组件--分页器(有用)

    一.分页器对象 from django.core.paginator import Paginator,EmptyPage book_list = Book.objects.all() #假设有100 ...