Codeforces483B——二分——Friends and Presents

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 cnt1cnt2xy (1 ≤ cnt1, cnt2 < 109cnt1 + 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.

Sample Input

Input

3 1 2 3

Output

5

Input

1 3 2 3

Output

4
/*
题目要求最大值的最小 再看看数据1e9肯定二分
推荐博文——http://www.cnblogs.com/windysai/p/4058235.html,写的很详细了,尤其那个图,没必要再阐述一遍

*/
#include <bits/stdc++.h>
using namespace std;

const long long  inf = 1e16+100;
long long  cnt1, cnt2, x, y;
bool check(long long  xx)
{
    long long  temp1 = xx/x;
    long long  temp2 = xx/y;
    long long  temp12 = xx/(x*y);
    long long  other = xx - temp1 - temp2 + temp12;
    long long  temp11 = temp1 - temp12;
    long long  temp22 = temp2 - temp12;
    long long  ans1 = cnt1 - temp22;
    long long  ans2 = cnt2 - temp11;
    if(ans1 < 0) ans1 = 0;
    if(ans2 < 0) ans2 = 0;
    return ans1 + ans2 <= other;
}

int main()
{
   while(~scanf("%lld%lld%lld%lld", &cnt1, &cnt2, &x, &y)){

       long long  l = 1, r = inf;
       while(l <= r){
          // printf("%lld %lld\n", l, r);
           long long  mid = l + r >> 1;
           if(!check(mid)){
               l = mid + 1  ;
           }
           else r = mid - 1;
       }
       printf("%lld\n", l);
   }
   return 0;
}

  

时间: 2024-10-20 14:17:55

Codeforces483B——二分——Friends and Presents的相关文章

Codeforces483B. Friends and Presents(二分+容斥原理)

题目链接:传送门 题目: B. Friends and Presents 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 cn

Codeforces 483B Friends and Presents(二分+数论)

题目链接:Codeforces 483B Friends and Presents 题目大意:要将1~v直间的数分配到两个集合中,第一个集合需要cnt1个数,第二个需要cnt2个数,第一个集合中的数 不能是x的倍数,同理第二个集合不能是y的倍数,两集合元素不能相同,问说v最小可以为多少. 解题思路:这题比第三题要难,想了有一会.二分答案,v,然后判断. 判断的时候只要分别判断集合一,二个数是否满足,但是因为有些数可以被分到两个集合,所以要判断总的可分配个数 是否满足大于cnt1+cnt2,计算总

Codeforces 483B - Friends and Presents - [二分]

题目链接:http://codeforces.com/contest/483 A - Counterexample - [简单构造题] Your friend has recently learned about coprime numbers. A pair of numbers $(a,?b)$ is called coprime if the maximum number that divides both $a$ and $b$ is equal to one. Your friend

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,

codeforces B. Friends and Presents(二分+容斥)

题意:从1....v这些数中找到c1个数不能被x整除,c2个数不能被y整除! 并且这c1个数和这c2个数没有相同的!给定c1, c2, x, y, 求最小的v的值! 思路: 二分+容斥,二分找到v的值,那么s1 = v/x是能被x整除的个数 s2 = v/y是能被y整除数的个数,s3 = v/lcm(x, y)是能被x,y的最小公倍数 整除的个数! 那么 v-s1>=c1 && v-s2>=c2 && v-s3>=c1+c2就是二分的条件! 1 #incl

2.1 二分分类

本周学习神经网络编程的基础知识 构建神经网络,有些技巧是非常重要 神经网络的计算过程中,通常有一个正向的过程(正向传播步骤),接着会有一个反向步骤(反向传播步骤), 为什么神经网络的计算可以分为前向传播和反向传播两个分开的过程?本周课程通过使用logistic回归来阐述,以便于能够更好的理解, logistic回归是一个用于二分分类的算法 比如有一个二分分类问题的例子, 假如有一张图像作为输入是这样的,你想输出识别此图的标签,如果是猫,输出1,如果不是,则输出0 使用y来表示输出的结果标签, 来

HDU3715(二分+2-SAT)

Go Deeper Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3184    Accepted Submission(s): 1035 Problem Description Here is a procedure's pseudocode: go(int dep, int n, int m)beginoutput the valu

二分查找

递归版(在区间[x, y)中找v的位置) 1 //递归版二分查找 2 int bsearch(int * A, int x, int y, int v) 3 { 4 5 if(v<a[x] || v>a[y-1]) return -1; 6 int m = x + (y-x)/2; //此处能不能用int m = (x+y)/2,需要仔细考虑(暂时想不到原因) 7 if(A[m]==v) return m; 8 else if(A[m]>v) return bsearch(A, x, m

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store