B. Friends and Presents

来源:互联网 发布:机器人编程入门教程 编辑:程序博客网 时间:2024/05/10 21:11

http://codeforces.com/contest/483/problem/B

大致题意:给4个数,a,b,x,y,求最小的v,将v里的数分成两组,一组不是x的倍数,一组不是y的倍数,要求第一组里的数的个数要大于等于a,第二组的个数大于等于b。

方法:把是x的倍数的放到第二组,是y倍数的放到第一组,如果即使x的倍数又是y的倍数,就两组都不放,其他的随便放,用二分判断满不满足条件,二分的上限就是(a+b)*2+1

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int a, b, x, y;int judge(__int64 pp){    __int64 qq =  (pp - pp/x - pp/y + pp/(x*y));    int cont1 = max(0, (int)(a-(pp/y-pp/(x*y))));    int cont2 = max(0, (int)(b-(pp/x-pp/(x*y))));    if(qq >= cont1+cont2)        return 1;    return 0;}__int64 solve(__int64 l, __int64 r){    __int64 res;    while(l <= r){        __int64 mid = (l+r) / 2;        if(judge(mid)){            res = mid;            r = mid - 1;        }        else            l = mid + 1;    }    return res;}int main(){    while(scanf("%d%d%d%d", &a, &b, &x, &y) == 4){        __int64 ans = solve(1, (a+b)*2+1);        printf("%I64d\n", ans);    }    return 0;}

0 0
原创粉丝点击