二分查找:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; typedef long long int LL; LL c1,c2,x,y; LL gcd(LL x,LL y) { if(x==0) return y; return gcd(y%x,x); } bool ck(LL n) { if(n-n/x<c1||n-n/y<c2) return false; LL all=n-n/(x/gcd(x,y)*y); if(all>=c1+c2) return true; return false; } LL bin() { LL low=1,high=120000000000LL,mid,ans; while(low<=high) { mid=(low+high)/2; if(ck(mid)) { ans=mid; high=mid-1; } else low=mid+1; } return ans; } int main() { cin>>c1>>c2>>x>>y; cout<<bin()<<endl; return 0; }