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. java思考题

    1.仔细阅读示例: EnumTest.java,运行它,分析运行结果? 你能得到什么结论?你掌握了枚举类型的基本用法了吗? public class EnumTest { public static ...

  2. BZOJ 1316: 树上的询问( 点分治 + 平衡树 )

    直接点分治, 用平衡树(set就行了...)维护. -------------------------------------------------------------------------- ...

  3. 关于已经安装python为何还要安装python-dev

    linux发行版通常会把类库的头文件和相关的pkg-config分拆成一个单独的xxx-dev(el)包. 以python为例, 以下情况你是需要python-dev的 你需要自己安装一个源外的pyt ...

  4. Git merge local repository

    I've met this problem for so many times and let me write a blog for solving this problem. First, you ...

  5. IList, ICollection ,IEnumerable AND IEnumerator in C#

    IList, ICollection ,IEnumerable 很显然,这些都是集合接口的定义,先看看定义: // 摘要: // 表示可按照索引单独访问的对象的非泛型集合. [ComVisible(t ...

  6. WebKit学习要点

    <体系结构详解> 1. 浏览器功能结构 2. 浏览器结构 3. Webkit体系结构 4. WebKit目录结构 5. WebKit编译 <HTML引擎详解> 1. HTML语 ...

  7. PHP入门-摘要表格处理问题

    几天来学习下来.PHP和C/C++有太多的阶段似系.所以,简单的入门现在看来已经没有问题.然而,由于所选择的条目是一个高速书籍,难免有些粗糙知识.例如,下面的两个问题让我吃了一些损失. 1. 文件标签 ...

  8. Your Job Is Not to Write Code

    I am lucky enough to work with a small team of fantastic engineers who truly care about their custom ...

  9. 快速构建ASP.NET MVC Admin主页

    前言 后台开发人员一般不喜欢调样式,搞半天样式出不来,还要考虑各种浏览器兼容,费心费力不讨好,还好互联网时代有大量的资源共享,避免我们从零开始,现在就来看怎么快速构建一个ASP.NET MVC后台管理 ...

  10. new[] class deconstructor

    Class class(); Class *class1=new class; class1=&class; delete class1;   // Assert 指针指向的是一个栈中的对象, ...