CodeForces 483B---Friends and Presents(二分)

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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值