CodeForces 483B Friends and Presents

Friends and Presents

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status Practice CodeForces 483B

Description

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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7
 8 typedef long long LL;
 9 LL cnt1, cnt2, x, y;
10
11 bool check(LL v)
12 {
13     LL f1,f2,both,others,ff1,ff2,gf1,gf2;
14      f1 = v / x;
15      f2 = v / y;
16      both = v / (x*y);
17      others = v - f1 - f2 + both;
18      ff1 = f1 - both;
19      ff2 = f2 - both;
20
21      gf1 = (cnt1 - ff2 >= 0 ? cnt1 - ff2 : 0);
22      gf2 = (cnt2 - ff1 >= 0 ? cnt2 - ff1 : 0);
23
24     return (gf1 + gf2 <= others);
25 }
26
27 int main()
28 {
29     while (scanf("%I64d%I64d%I64d%I64d", &cnt1, &cnt2, &x, &y) != EOF)
30     {
31         LL l=1, r=1e18;
32         while (l<r)
33         {
34             LL m=(l+r)/2;
35             if (check(m))
36                 r=m;
37             else
38                 l=m+1;
39         }
40         printf("%I64d\n",r);
41     }
42     return 0;
43 }

时间: 2024-10-14 14:34:16

CodeForces 483B Friends and Presents的相关文章

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(二分+容斥)

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 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

1 #include<cstdio> 2 using namespace std; 3 long long m,cnt1,cnt2,x,y; 4 long long gcd(long long a,long long b) 5 { 6 if (b==0) return a; 7 return gcd(b,a % b); 8 } 9 long long lcm(long long a,long long b) 10 { 11 return a*b/gcd(a,b); 12 } 13 bool f

Codeforces 1090C New Year Presents

New Year Presents 用set模拟一下.. 写的bug有点多. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PII pair<int, int> using namespace std; const int N = (int)1e5 + 7; int n, m, sum, min_cnt, tar

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

cft1B

B - Friends and Presents Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 483B Description You have two friends. You want to present each of them several positive integers. You want to pr

Educational Codeforces Round 79 (Rated for Div. 2) C. Stack of Presents

链接: https://codeforces.com/contest/1279/problem/C 题意: Santa has to send presents to the kids. He has a large stack of n presents, numbered from 1 to n; the topmost present has number a1, the next present is a2, and so on; the bottom present has numbe

[Codeforces Round #275 (Div. 2)]B - Friends and Presents

最近一直在做 codeforces ,总觉得已经刷不动 BZOJ 了? ——真是弱喵 你看连 Div.2 的 B 题都要谢谢题解,不是闲就是傻 显然我没那么闲 ╮(╯_╰)╭ 我觉得这题的想法挺妙的~ 大意是你需要分别给 a 和 b cnt1 和 cnt2 个数字 但是 a 不要被 x 整除的数 ,as well as,b 不要被 y 整除的数 然后求需要给的最大数的最小值—— 最值的最值?那不是典型的二分吗? 但是——坑爹的英文题导致我完全没有意识到这一点…… 二分答案 v ,因为 a 不要被