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

3199 단어 codeforces
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.
2점 답안을 하고 x와 y의 공배수를 빼고 x의 배수 개수를 두 번째 사람에게 주고 y의 배수 개수를 첫 번째 사람에게 주고 판단하면 된다. 시합할 때 2점의 상한선을 작게 쓰고 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
", ans); } return 0; }

좋은 웹페이지 즐겨찾기