zoukankan      html  css  js  c++  java
  • CodeForces 483B 二分答案

    题目:

    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You have two friends. You want to present each of them several positive integers. You want to present cnt1 numbers to the first friend and cnt2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.

    In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.

    Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.

    A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.

    Input

    The only line contains four positive integers cnt1, cnt2, xy (1 ≤ cnt1, cnt2 < 109; cnt1 + cnt2 ≤ 109; 2 ≤ x < y ≤ 3·104) — the numbers that are described in the statement. It is guaranteed that numbers xy are prime.

    Output

    Print a single integer — the answer to the problem.

    Examples
    input
    3 1 2 3
    output
    5
    input
    1 3 2 3
    output
    4
    Note

    In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.

    In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.

    题解:

    n-n/x-n/y+n/xy这一部分是两边都能用的,而n/y-n/xy是只能给x集合用的,n/x-n/xy是只能给y集合用的,先分配这两个,分配完之后剩下的再用n-n/x-n/y+n/xy里面的去填。满足条件的话就二分更小的,否则,二分更大的。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<map>
     6 #include<utility>
     7 #include<vector>
     8 #include<algorithm>
     9 using namespace std;
    10 typedef long long LL;
    11 
    12 const LL maxn = 2e11;
    13 
    14 LL cnt1, cnt2, x, y;
    15 
    16 bool ok(LL n, LL t1, LL t2) {
    17     t1 = t1 - (n / y - n / (x*y)); if (t1<0) t1 = 0;
    18     t2 = t2 - (n / x - n / (x*y)); if (t2<0) t2 = 0;
    19     if (n - n / x - n / y + n / (x*y) >= t1 + t2) return true;
    20     return false;
    21 }
    22 
    23 int main() {
    24     //    freopen("data_in.txt","r",stdin);
    25     while (scanf("%lld%lld%lld%lld", &cnt1, &cnt2, &x, &y) == 4) {
    26         LL low = 0, hig = maxn;
    27         //区间(low,hig]
    28         while (low + 1<hig) {
    29             LL mid = low + (hig - low) / 2;
    30             if (ok(mid, cnt1, cnt2)) {
    31                 hig = mid;
    32             }
    33             else {
    34                 low = mid;
    35             }
    36         }
    37         printf("%lld
    ", hig);
    38     }
    39     return 0;
    40 }
  • 相关阅读:
    java 之 xml
    oracle 之 定时任务,存储过程和游标等语法案例
    oracle 之创建定时器
    java 之 音乐播放代码
    oracle函数之 minus
    iis默认文档有什么用?
    html页面使用前端框架布局时,弹出子页面等情况若出现布局混乱,可能是回发导致
    html 之 区块元素属性(待补充)
    EffectiveJava(2)应对多个构造函数应当使用构建器
    EffectiveJava(1) 构造器和静态工厂方法
  • 原文地址:https://www.cnblogs.com/fenice/p/5449639.html
Copyright © 2011-2022 走看看