codeforces 483b Friends and Presents 二分

题意:

思路:

当时就想到用二分,但是二分的判断一直没想好,具体看代码吧。而且,看到好多人FST 第9组数据= =

二分的范围要注意。我直接右边的范围就设为10^9,结果就WA了。

注意使用long long;

code:

/* **********************************************
Created Time: 2014-10-25 14:58:22
File Name   : cf.cpp
*********************************************** */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <functional>
#include <algorithm>
using namespace std;
typedef long long LL;
LL cot1, cot2, x, y;
bool charge(LL n)
{
        LL t1 = n/x, t2 = n/y;
        LL t3 = n/(x*y);
        LL gx = n - t1 - t2 + t3;
        LL c1 = cot1 - (t2-t3), c2 = cot2 - (t1-t3);
        if(c1 > 0)
                gx -= c1;
        if(gx < 0)
                return false;
        if(c2 > 0)
                gx -= c2;
        if(gx < 0)
                return false;
        return true;
}
int main()
{
        while(cin>>cot1>>cot2>>x>>y)
        {
                LL l = cot1+cot2, r = (LL)1e15;
                LL ans = 0;
                while(l <= r)
                {
                        LL mid = (l+r)/2;
                        if(charge(mid))
                        {
                                ans = mid;
                                r = mid-1;
                        }
                        else
                                l = mid+1;
                }
                cout<<ans<<endl;
        }
        return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值