CodeForces 483B Friends and Presents(二分)

题目思路

看懂题意之后一直在想怎么模拟
但又感觉模拟不出来 数据范围也有到1e9 答案也到了1e18
想了挺久都不知道写 问了下zw
说是二分 但我怎么看都知道咋二分
然后就去看题解了(啥时候我能不看题解a题啊 太菜了我)
题解是直接将区间数据抽象成一张图
具体分析见这个链接(画不来图)
https://www.cnblogs.com/windysai/p/4058235.html
感觉真的好巧妙啊

ac代码

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#define pi 3.1415926535898
#define ll long long
#define lson rt<<1
#define rson rt<<1|1
#define eps 1e-6
#define ms(a,b) memset(a,b,sizeof(a))
#define legal(a,b) a&b
#define print1 printf("111\n")
using namespace std;
const int maxn = 2e2+10;
const int inf = 0x3f3f3f3f;
const ll llinf =0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;

ll n,m,x,y;
ll ans;

bool check(ll r)
{
    ll f1=r/x;
    ll f2=r/y;
    ll in=r/(x*y);
    ll F1=r/y-in;
    ll F2=r/x-in;
    ll out=r-f1-f2+in;
    ll tem1=max(n-F1,(ll)0);
    ll tem2=max(m-F2,(ll)0);
    if(tem1+tem2<=out)
    {
        return true;
    }else
        return false;
}
int main()
{

    scanf("%lld%lld%lld%lld",&n,&m,&x,&y);
    ll l=0,r=1e18;
    while(l<=r)
    {
        ll mid=(l+r)>>1;
        if(check(mid))
        {
            ans=mid;
            r=mid-1;
        }else
        {
            l=mid+1;
        }
    }
    printf("%lld\n",ans);
}

猜你喜欢

转载自blog.csdn.net/daydreamer23333/article/details/107673747