Codeforces Round #275 (Div. 2) B. Friends and Presents 二分+数学
2014-10-31 13:46
513 查看
8493833 | 2014-10-31 08:41:26 | njczy2010 | B - Friends and Presents | GNU C++ | Accepted | 31 ms | 4 KB |
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, x, y (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 x, y are prime.
Output
Print a single integer — the answer to the problem.
Sample test(s)
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.
#include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<algorithm> #include<cmath> #include<queue> #include<map> #include<set> #include<string> //#include<pair> #define N 3000 #define M 1005 #define mod 1000000007 //#define p 10000007 #define mod2 100000000 #define ll long long #define LL long long #define maxi(a,b) (a)>(b)? (a) : (b) #define mini(a,b) (a)<(b)? (a) : (b) using namespace std; ll cnt1,cnt2,x,y; ll c; ll tot; void ini() { tot=cnt1+cnt2; } ll gcd(ll a,ll b) { if(b==0){ return a; } else return gcd(b,a%b); } ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } ll ok(ll m) { ll c1,c2,c3; c1=m-m/x; c2=m-m/y; c3=m-m/x-m/y+m/c; //printf(" c1=%I64d c2=%I64d c3=%I64d c=%I64d m=%I64d\n",c1,c2,c3,c,m); if(c1>=cnt1 && c2>=cnt2 && c1+c2-c3>=tot){ return 1; } else{ return 0; } } void solve() { c=lcm(x,y); // printf(" c=%I64d\n",c); ll l,r,m; l=1,r=20000000009; m=(l+r)/2; while(l<r) { // printf(" l=%I64d r=%I64d m=%I64d\n",l,r,m); if(ok(m)==1){ r=m; } else{ l=m+1; } m=(l+r)/2; } // printf(" l=%I64d r=%I64d m=%I64d\n",l,r,m); printf("%I64d\n",m); } void out() { } int main() { //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); // scanf("%d",&T); // for(int ccnt=1;ccnt<=T;ccnt++) // while(T--) while(scanf("%I64d%I64d%I64d%I64d",&cnt1,&cnt2,&x,&y)!=EOF) { // if(n==0 && m==0 ) break; //printf("Case %d: ",ccnt); ini(); solve(); out(); } return 0; }
相关文章推荐
- Codeforces Round #275 (Div. 2) --B Friends and Presents
- Codeforces Round #253 (Div. 2) D. Andrey and Problem (贪心+数学概率)
- Codeforces Round #311 (Div. 2) B. Pasha and Tea【二分】
- Codeforces Round #280 (Div. 2) E. Vanya and Field (数学GCD)
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分
- Codeforces Round #201 (Div. 1) A. Alice and Bob(数学)
- Codeforces Round #272 (Div. 2) C. Dreamoon and Sums (数学 思维)
- Codeforces Round #227 (Div. 2) E. George and Cards set内二分+树状数组
- Codeforces Round #182 (Div. 1) B. Yaroslav and Time(二分+SPFA变形)
- Codeforces Round #330 (Div. 2)——B 数学——Pasha and Phone
- Codeforces Round #280 (Div. 2) D. Vanya and Computer Game 数学+预处理
- Codeforces Round #272 (Div. 2) C Dreamoon and Sums(数学)
- Codeforces Round #305 (Div. 1)E. Mike and Friends 后缀数组+RMQ+线段树
- Codeforces Round #281 (Div. 2) C. Vasya and Basketball 二分
- Codeforces Round #293 (Div. 2) C. Anya and Smartphone 数学题
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)——A二分——Bear and Elections
- Codeforces Round #259 (Div. 1) A. Little Pony and Expected Maximum 数学公式结论找规律水题
- Codeforces Round #285 (Div.1 B & Div.2 D) Misha and Permutations Summation --二分+树状数组
- Codeforces Round #272 (Div. 2) C. Dreamoon and Sums 数学
- Codeforces Round #299 (Div. 1) A. Tavas and Karafs(二分)