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. 小记 xian80 坐标转换 wgs84

    转坐标这个问题是个老生常谈的话题了. 昨天遇到同事求助将 xian80的平面坐标转换到2000下. 想了一下,因为暂时还没有现成的2000的dwg数据可用,只能暂时以wgs84的为准了,然而有个问题, ...

  2. iOS----------随机色

    #define KColorRandomColor [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 ...

  3. Python Learning: 01

    After a short period of  new year days, I found life a little boring. So just do something funny--Py ...

  4. tomcat设置开机启动

    一.windows 1. 下载tomcat 2. 进入bin目录,查看是否存在service.dat,如果没有自行创建 3. 打开cmd,进入tomcat>bin目录 说明:用法: servic ...

  5. Java调用windows命令

    JAVA调用windows的cmd命令 用起来会让程序变得更加简洁明了,非常实用. 核心就是使用 Runtime类. cmd的xcopy就有很强大的文件夹,文件处理功能. 下面就以xcopy来说明,如 ...

  6. Nginx反向代理实现IP访问分流

    通过Nginx做反向代理来实现分流,以减轻服务器的负载和压力是比较常见的一种服务器部署架构.本文将分享一个如何根据来路IP来进行分流的方法. 根据特定IP来实现分流 将IP地址的最后一段最后一位为0或 ...

  7. MySql 学习之路-高级2

    目录: 1.约束 2.ALTER TABLE 3.VIEW 1.约束 说明:SQL约束用于规定表中的数据规则,如果存在违反约束的数据行为,行为会被约束终止,约束可以在建表是规定,也可以在建表后规定,通 ...

  8. consul 搭建

    windows 1. 下载consul https://www.consul.io/downloads.html 2. 解压至consul_1.4.2 3.配置环境变量 path下新增D:\work\ ...

  9. SQL CREATE TABLE 语句

    CREATE TABLE 语句 CREATE TABLE 语句用于创建数据库中的表. SQL CREATE TABLE 语法 CREATE TABLE 表名称 ( 列名称1 数据类型, 列名称2 数据 ...

  10. TPYBoard开发板搭建,实现隐秘通信

    一.准备工作 lTPYBoard v102(简称v102) 1块 lTPYBoard v202(简称v202) 1块 l杜邦线.MicroUSB数据线 若干 (成本100元以内,某宝上可以买到) 附上 ...