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.

 #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==){
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 ;
}
else{
return ;
}
} void solve()
{
c=lcm(x,y);
// printf(" c=%I64d\n",c);
ll l,r,m;
l=,r=;
m=(l+r)/;
while(l<r)
{
// printf(" l=%I64d r=%I64d m=%I64d\n",l,r,m);
if(ok(m)==){
r=m;
}
else{
l=m+;
}
m=(l+r)/;
}
// 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 ;
}

最新文章

  1. JSONP实现
  2. 简单的后台json,前台解析 操作
  3. Spring容器初始化过程
  4. [moka学习笔记]yii2.0数据库查询的多种方法(未完待整理)
  5. [Bug]The maximum array length quota (16384) has been exceeded while reading XML data.
  6. 用Spring3编写第一个HelloWorld项目
  7. XtraBackup原理4
  8. 【jar包】Android——eclipse共享library以及导出jar包
  9. node中使用es6/7/8 --- 支持性与性能
  10. 灾难恢复-boot分区的恢复方法
  11. linq分组求和_实体类和datatable
  12. SQL遇到的问题
  13. python实现列表去重的方法
  14. BZOJ 2784 时间流逝
  15. CentOS 6.8 源码安装RabbitMQ
  16. Java笔试面试题整理第一波
  17. 记录一次优化mysql查询语句的方法
  18. unity3d摄像机参数
  19. django+echarts
  20. mybatis 批量修改接口的几种实现方式

热门文章

  1. 基于Python的Web应用开发实战——2 程序的基本结构
  2. Django-C003-视图
  3. PHP的PDF扩展库TCPDF将中文字体设置为内嵌字体的方法
  4. SpringMVC+Spring+Mybatis整合程序之整合
  5. iOS 设计模式
  6. dubbo---------timeout与retires
  7. verdi知识点
  8. sed快速学习
  9. verilog behavioral modeling--overview
  10. 王小胖之 Base64编码/解码