本文共 1023 字,大约阅读时间需要 3 分钟。
/********************************************************************************* 题意:一个人有两个 friends,需要将 a个不能整除 x 的数分给第一个friend,**** b 个不能整除 y 的数分给第二个friend。x 和 y 都是素数来的。**** 要求求出最小的 w,w表示可以从1,2,...,w 中取数。**** 题解:二分查找【1,1e18】*****************************************************************************/#include#include #include #include #include using namespace std;typedef long long LL;LL x,y,a,b;bool check(LL w){ LL x1 = w / x; LL x2 = w / y; LL B = w /(x * y); LL A = x1 - B; LL C = x2 - B; LL shengyu = w - A - B - C; LL a1 , b1; if(a > C) a1 = a - C; else a1 = 0; if(b > A) b1 = b - A; else b1 = 0;// cout << shengyu << endl; if(shengyu >= a1 + b1) return true; return false;}int main(){ cin >> a >> b >> x >> y; LL l = 1,r = 1e18;// cout << r << endl; LL mid; while(l < r){ mid = (l + r ) / 2; if(check(mid)) r = mid; else l = mid + 1; } cout << r << endl;}