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. Js闭包与循环

    目标:点击任何一个li,提示当前点击位置 <ul> <li>第1个</li> <li>第2个</li> <li>第3个</ ...

  2. JavaWeb解释一下什么是 servlet?

    Servlet是一种独立于平台和协议的服务端的java技术,可以生成动态WEB页面与传统的CGI(计算机图形接口)和其他类似的CGI技术相比.Servlet具有更好的可移植性.更强大的功能,更少的投资 ...

  3. Hadoop学习笔记(4)hadoop集群模式安装

    具体的过程参见伪分布模式的安装,集群模式的安装和伪分布模式的安装基本一样,只有细微的差别,写在下面: 修改masers和slavers文件: 在hadoop/conf文件夹中的配置文件中有两个文件ma ...

  4. Java学习之异常处理

    在 Java 中,所有的异常都有一个共同的祖先 Throwable(可抛出),Throwable 指定代码中可用异常传播机制通过 Java 应用程序传输的任何问题的共性.       Throwabl ...

  5. [LeetCode]题解(python):096-Unique Binary Search Trees

    题目来源: https://leetcode.com/problems/unique-binary-search-trees/ 题意分析: 给定一个整数n,返回所有中序遍历是1到n的树的可能. 题目思 ...

  6. appium 学习各种小功能总结--功能有《滑动图片、保存截图、验证元素是否存在、》---新手总结(大牛勿喷,新手互相交流)

    1.首页滑动图片点击 /** * This Method for swipe Left * 大距离滑动 width/6 除数越大向左滑动距离也越大. * width:720 *height:1280 ...

  7. IO调度算法研究1

    linux kernel 2.6之后提供了四种IO调度算法,每种调度算法都有其不同的特点和应用场景,系统使用者可以通过系统提供的接口,选择使用哪种IO调度算法,以及调整IO调度算法的参数,以达到最优的 ...

  8. 扩展ASP.NET MVC HtmlHelper类

    在这篇帖子中我会使用一个示例演示扩展ASP.NET MVC HtmlHelper类,让它们可以在你的MVC视图中工作.这个示例中我会提供一个简单的方案生成Html表格. HtmlHelper类 Htm ...

  9. 射频识别技术漫谈(14)——Mifare S50与S70的存取控制

    存取控制指符合什么条件才能对卡片进行操作. S50和S70的块分为数据块和控制块,对数据块的操作有“读”.“写”.“加值”.“减值(含传输和存储)”四种,对控制块的操作只有“读”和“写”两种. S50 ...

  10. JavaEE Tutorials (9) - 运行持久化示例

    9.1order应用118 9.1.1order应用中的实体关系119 9.1.2order应用中的主键121 9.1.3实体映射到多个数据库表125 9.1.4order应用中的层叠操作125 9. ...