现在的位置: 首页 > 综合 > 正文

Codeforces Round #275 (Div. 2)B——Friends and Presents

2019年02月17日 ⁄ 综合 ⁄ 共 2132字 ⁄ 字号 评论关闭
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
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.

二分答案来做,去掉x和y的公倍数,把x的倍数个数给到第二个人,把y的倍数个数给到第一个人,然后判断即可,比赛时二分的上限写小了然后FST了,瞬间哭晕在厕所

#include <map>  
#include <set>  
#include <list>  
#include <stack>  
#include <vector>  
#include <queue>  
#include <cmath>  
#include <cstdio>  
#include <cstring>  
#include <iostream>  
#include <algorithm>  

using namespace std;

int main()
{
	__int64 cnt1, cnt2, x, y;
	while (~scanf("%I64d%I64d%I64d%I64d", &cnt1, &cnt2, &x, &y))
	{
		__int64 l, r, mid;
		__int64 tmp, s1, s2, s3, z = x * y, tmp1, tmp2, ans = 0;
		l = 1;
		r = 2000000000;
		while (l <= r)
		{
			tmp1 = cnt1;
			tmp2 = cnt2;
			mid = (l + r) / 2;
			s1 = mid / x;
			s2 = mid / y;
			s3 = mid / z;
			s1 -= s3;
			s2 -= s3;
			tmp = mid - s3;
			tmp1 -= s2;
			if (tmp1 < 0)
			{
				tmp1 = 0;
			}
			tmp -= s2;
			tmp2 -= s1;
			if (tmp2 < 0)
			{
				tmp2 = 0;
			}
			tmp -= s1;
			if (tmp >= tmp1 + tmp2)
			{
				r = mid - 1;
				ans = mid;
			}
			else
			{
				l = mid + 1;
			}
		}
		printf("%I64d\n", ans);
	}
	return 0;
}

抱歉!评论已关闭.