又是一道涨姿势的题目
这个同学解释的很好,附链接:http://www.cnblogs.com/windysai/p/4058235.html
画个韦恩图的话就很直观了
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
#define maxn 1e18
LL cnt1,cnt2,x,y;
bool solve(LL m){
LL f1=m/x; //能被x整除的数的个数
LL f2=m/y; //能被y整除的数的个数
LL both=m/(x*y); //既能被y也能被x整除的数的个数
LL others=m-f1-f2+both; //不能被y也不能被x整除的数的个数
LL ff1=f1-both; //只能被f1整除的数的个数
LL ff2=f2-both; //只能被f2整除的数的个数
LL gf1=(cnt1-ff2>=0 ? cnt1-ff2 : 0); //在others里能选择出可以给第一个人的数的数字个数
LL gf2=(cnt2-ff1>=0 ? cnt2-ff1 : 0); //在others里能选择出可以给第二个人的数的数字个数
return (gf1+gf2<=others);
}
int main(){
while(cin>>cnt1>>cnt2>>x>>y){
LL l=1,r=1e18;
while(l<r){
LL m=(l+r)>>1;
if(solve(m))
r=m;
else
l=m+1;
}
cout<<r<<endl;
}
return 0;
}