首页 > 代码库 > Codeforces 483B - Friends and Presents(二分+容斥)

Codeforces 483B - Friends and Presents(二分+容斥)

483B - Friends and Presents

思路:这个博客写的不错:http://www.cnblogs.com/windysai/p/4058235.html

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset((a),(b),sizeof(a))
const ll INF=1e18;
ll c1,c2,x,y;

bool check(ll v)
{
    ll f1=v/x;//整除x 
    ll f2=v/y;///整除y 
    ll b=v/(x*y);//都整除
    ll _f1=f1-b;//只整除x 
    ll _f2=f2-b;//只整除y
    ll o=v-_f1-_f2-b;//都不整除
    
    ll t1=c1-_f2;//还需要的个数 
    if(t1<=0)t1=0;
    ll t2=c2-_f1;//还需要的个数 
    if(t2<=0)t2=0;
    
    return (t1+t2<=o);
}

int main()
{
    cin>>c1>>c2>>x>>y;
    
    ll l=1,r=INF,m;
    while(l<r)
    {
        m=(l+r)>>1;
        if(check(m))r=m;
        else l=m+1;
    }
    m=(l+r)>>1;
    cout<<m<<endl;
    return 0;
}

 

Codeforces 483B - Friends and Presents(二分+容斥)