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 andcnt2 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, xy (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 xy 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.

二分渣渣把二分又写跪了,总是分不清l与r的关系o(╯□╰)o,我居然l和r都推断了一下。这题居然l和r都能过。

这题就是枚举一下中间结果,对a周期为x-1,b周期为y-1,仅仅有当i为y的倍数时,仅仅能让a取,当i为x的倍数时,仅仅

能让b取,算一下x,y的倍数时两个都不能取得,a的总数量减去仅仅能a取的,b的总数量减去仅仅能b取的 ,剩下的要

取的和小于等于两个都能取得,这个值就是有效值。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
long long cnt1,cnt2,x,y;
long long gcd(long long a,long long b)
{
return b==0?a:gcd(b,a%b);
}
bool judge(long long v)
{
long long t1,t2,t3;
t1=v/x;
t2=v/y;
t3=v/(x*y/gcd(x,y));
long long temp1,temp2,temp3;
temp1=max((long long)0,cnt1-t2+t3);//t2-t3是仅仅能a取的,cnt1-仅仅能a取的,就是剩下a没取的
temp2=max((long long)0,cnt2-t1+t3);//t1-t3是仅仅能b取的,cnt2-仅仅能b取的,就是剩下b没取的
temp3=max((long long)0,v-t1-t2+t3);//x,y都能取的
if(temp3>=temp1+temp2)//a,b都能取的大于要大于等于a,b没取的和
return true;
else
return false;
}
int main()
{
scanf("%I64d%I64d%I64d%I64d",&cnt1,&cnt2,&x,&y);
long long l=1,r=2000000000;
long long u=0;
while(l<r)
{
int m=(l+r)>>1;
if(judge(m))
{
u=m;
r=m;
}
else
{
l=m+1;
}
}
// long long ans;
// if(judge(r))
// ans=r;
// else
// ans=l;
printf("%I64d\n",u);
return 0;
}

B. Friends and Presents(Codeforces Round #275(div2)的更多相关文章

  1. A. Counterexample (Codeforces Round #275(div2)

    A. Counterexample time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  2. C. Diverse Permutation(Codeforces Round #275(div2)

    C. Diverse Permutation time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

    题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...

  4. Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造

    Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 ht ...

  5. 构造 Codeforces Round #275 (Div. 2) C. Diverse Permutation

    题目传送门 /* 构造:首先先选好k个不同的值,从1到k,按要求把数字放好,其余的随便放.因为是绝对差值,从n开始一下一上, 这样保证不会超出边界并且以防其余的数相邻绝对值差>k */ /*** ...

  6. Codeforces Round #539 div2

    Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...

  7. 【前行】◇第3站◇ Codeforces Round #512 Div2

    [第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...

  8. Codeforces Round#320 Div2 解题报告

    Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...

  9. Codeforces Round #564(div2)

    Codeforces Round #564(div2) 本来以为是送分场,结果成了送命场. 菜是原罪 A SB题,上来读不懂题就交WA了一发,代码就不粘了 B 简单构造 很明显,\(n*n\)的矩阵可 ...

随机推荐

  1. ajax验证码检测

    1.验证码文件 <%@ page language="java" pageEncoding="UTF-8"%> <%@ page conten ...

  2. POJ 3268 Silver Cow Party 正反图最短路

    题目:click here 题意: 给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都 ...

  3. Laravel5 学习与使用(一)

    2015-07-04 (1)  安装Laravel框架 ① 安装前的准备工作 使用Apache24 + PHP 5.6 + MySQL 开发环境完成PHP网站开发,所以Laravel的安装是建立在以上 ...

  4. 腾讯云部署Flask应用

    由于新浪云现在不免费了.而且云豆也用完了.所以去腾讯云申请了个学生云主机,一元一个月. 不过部署开发环境还是有点麻烦的,搞了好几天,终于部署成功了! 下面说部署过程: 我云主机用的是 Ubuntu 1 ...

  5. Immediate Decodability问题Java解答

    DescriptionAn encoding of a set of symbols is said to be immediately decodable if no code for one sy ...

  6. SqlHelp

    using System.Configuration;using System.Data; public class SqlHelp { private static string connectio ...

  7. struts的MVC详细实现

    struts中核心类:ActionServlet,其中最主要的操作是RequestProcessor类中的process方法. struts实现mvc的详解图如下: 1.Tomcat一启动,根据web ...

  8. Zencart先生成订单后付款,类似淘宝后台修改订单价格

    Zencart 使用 Paypal 付款,会出现漏单的情况,即 paypal 已经收到客户的付款,但是网站后台没有客户的订单.导致 paypal 漏单的原因大致会是当客户跳转到Paypal 网站付款完 ...

  9. Python学习笔记 (2) :字符串输出、操作、格式化和日期、时间格式化

    一.字符串输出及运算 1.常用输出格式及方法 ')#单引号 ")#双引号 """)#三个引号 1234567890 1234567890 1234567890 ...

  10. QT不让windows休眠的方法

    对于一些Windows应用程序,必须要保证os不能休眠才能有效工作,如迅雷下载软件,如果os进入休眠,则会导致网络不正常,从而导致不能下载东西.那木有没有1种机制,当打开软件的时候,就自动将os设为不 ...