Codeforces 483B - Friends and Presents(二分)

来源:互联网 发布:南京java培训 知乎 编辑:程序博客网 时间:2024/05/02 13:39

        又是一道涨姿势的题目

  这个同学解释的很好,附链接:http://www.cnblogs.com/windysai/p/4058235.html

画个韦恩图的话就很直观了

#include <iostream>#include <cstdio>#include <cmath>using namespace std;typedef long long LL;#define maxn 1e18LL 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;elsel=m+1;}cout<<r<<endl;}return 0;}

0 0