https://vjudge.net/problem/CodeForces-483B/origin
Description
You have two friends. You want to present each of them several positive integers. You want to present cnt 1 numbers to the first friend and cnt 2 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 cnt 1, cnt 2, x, y (1 ≤ cnt 1, cnt 2 < 109; cnt 1 + cnt 2 ≤ 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.
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
题意:
要求在1-v中,需要有cnt1个不被x整除的数,和cnt2个不被y整除的数,并且,cnt1,cnt2这两类数不能重复。我们需要求出v。
a表示在1-v中能被x整除的数
b表示在1-v中能被y整除的数
a+b中的数只能使用一次
我们需要的是v减去这些数能大于等于cnt1 + cnt2
a=v/x;
b=v/y;
a+b=v/x/y;
我们要求不能被x,y整除的,那么就直接把范围内能被x,y整除的求出来,然后减到,看是否满足题目要求。
代码注意要用 long long
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <map>
using namespace std;
const int inf=0x3f3f3f3f;
int cnt1,cnt2,x,y;
int main()
{
ios::sync_with_stdio(false);
while (cin>>cnt1>>cnt2>>x>>y) {
long long l=1,r=10000000000,mid=0;
while (l<r) {
mid=(l+r)/2;
if((mid-mid/x>=cnt1)&&(mid-mid/y>=cnt2)&&(mid-mid/x/y>=cnt1+cnt2))
r=mid;
else
l=mid+1;
}
cout<<l<<endl;
}
return 0;
}