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, xy (1 ≤ cnt1, cnt2 < 10^9; cnt1 + cnt2 ≤ 10^9; 2 ≤ x < y ≤ 3·10^4) — 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
感想:其实直接二分就行了,但是我分类了好一会儿,直接算的
思路:
分成四种情况,1 不能被x,y整除(a) 2 不能被x整除(b) 3 不能被x整除(c) 4 同时能被x,y整除
那么就需要满足
a+b>=cnt2
a+c>=cnt1
a+b+c>=cnt1+cnt2
于是算来算去就算出来了,还是二分好用
 
#include<cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
ll x,y,cnt1,cnt2;
ll gcd(ll a,ll b){
if(b==0)return a;
return gcd(b,a%b);
}
ll pos(ll a){
if(a>=0)return a;
return 0;
}
int main(){
scanf("%I64d%I64d%I64d%I64d",&cnt1,&cnt2,&x,&y);
ll d=gcd(x,y);
ll lcm=x*y/d;
ll na=lcm-lcm/x-lcm/y+1;
ll nb=lcm/x-1;
ll nc=lcm/y-1;
ll sumn=na+nb+nc;
ll t=(cnt1+cnt2)/sumn;
t=max(t,cnt2/(na+nb));
t=max(t,cnt1/(na+nc));
ll ta=na*t;
ll tb=nb*t;
ll tc=nc*t;
ll ans=lcm*t;
if((pos(cnt2-tb)+pos(cnt1-tc))<=ta)ans--;
else {
ll r=0x7fffffff;
ll r0=pos(cnt2-tb)+pos(cnt1-tc)-ta;
if(r0>=0&&cnt2>=tb&&cnt1>=tc)r=min(r,r0); ll r1=x*(cnt1-tc-ta)/(x-1);
ll tr11=cnt1-tc-ta+(r1/x-1);
if(tr11/x==r1/x-1)r1=min(r1,tr11);
r1=max(r1,x*(cnt2-tb));
if(cnt1>=tc+ta)r=min(r,r1);
ll r2=y*(cnt2-tb-ta)/(y-1);
ll tr2=cnt2-tb-ta+(r2/y-1);
if(tr2/y==r2/y-1)r2=min(r2,tr2);
r2=max(r2,y*(cnt1-tc));
if(cnt2>=tb+ta)r=min(r,r2);
ans+=r;
}
printf("%I64d\n",ans);
return 0;
}

  

 

CF 483B. Friends and Presents 数学 (二分) 难度:1的更多相关文章

  1. Codeforces 483B - Friends and Presents(二分+容斥)

    483B - Friends and Presents 思路:这个博客写的不错:http://www.cnblogs.com/windysai/p/4058235.html 代码: #include& ...

  2. 快速切题CF 158B taxi 构造 && 82A double cola 数学观察 难度:0

    实在太冷了今天 taxi :错误原因1 忽略了 1 1 1 1 和 1 2 1 这种情况,直接认为最多两组一车了 2 语句顺序错 double cola: 忘了减去n的序号1,即n-- B. Taxi ...

  3. CodeForces 483B Friends and Presents

     Friends and Presents Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I ...

  4. Codeforces483B. Friends and Presents(二分+容斥原理)

    题目链接:传送门 题目: B. Friends and Presents time limit per test second memory limit per test megabytes inpu ...

  5. UVA 11881 Internal Rate of Return(数学+二分)

    In finance, Internal Rate of Return (IRR) is the discount rate of an investment when NPV equals zero ...

  6. HDU 6216 A Cubic number and A Cubic Number(数学/二分查找)

    题意: 给定一个素数p(p <= 1e12),问是否存在一对立方差等于p. 分析: 根据平方差公式: 因为p是一个素数, 所以只能拆分成 1*p, 所以 a-b = 1. 然后代入a = b + ...

  7. codeforces 483B Friends and Presents 解题报告

    题目链接:http://codeforces.com/problemset/problem/483/B 题目意思:有两个 friends,需要将 cnt1 个不能整除 x 的数分给第一个friend, ...

  8. UVA 10668 - Expanding Rods(数学+二分)

    UVA 10668 - Expanding Rods 题目链接 题意:给定一个铁棒,如图中加热会变成一段圆弧,长度为L′=(1+nc)l,问这时和原来位置的高度之差 思路:画一下图能够非常easy推出 ...

  9. CF 551E. GukiZ and GukiZiana [分块 二分]

    GukiZ and GukiZiana 题意: 区间加 给出$y$查询$a_i=a_j=y$的$j-i$最大值 一开始以为和论文CC题一样...然后发现他带修改并且是给定了值 这样就更简单了.... ...

随机推荐

  1. 【Maven学习】Nexus私服代理其他第三方的Maven仓库

    一.背景 [Maven学习]Nexus OSS私服仓库的安装和配置 http://blog.csdn.net/ouyang_peng/article/details/78793038 [Maven学习 ...

  2. Logback配置讲解

    复制文件并粘贴到项目下: logback.xml: <?xml version="1.0" encoding="UTF-8"?> <confi ...

  3. [py][mx]django实现课程机构排名

    如果是第一次做这个玩意,说实话,确实不知道怎么弄, 做一次后就有感觉了 此前我们已经完成了: 分类筛选 分页 这次我们做的是 课程机构排名 知识点: - 按照点击数从大到小排名, 取出前三名 hot_ ...

  4. 入坑-DM导论-第一章绪论笔记

    //本学习笔记只是记录,并未有深入思考. 1.什么是数据挖掘? 数据挖掘是数据库中发现必不可少的一部分. 数据预处理主要包括(可能是最耗时的步骤): 1.融合来自多个数据源的数据 2.清洗数据以消除噪 ...

  5. 模块讲解----configparser模块(my.cnf配置文件操作)

    查询 1.所有节点: 2.指定节点下的所有key和values: 3.指定节点下所有的key: 4.指定节点和key下的values: # #configparser用于处理特定格式的文件,其本质上是 ...

  6. linux更改文件或目录的属主和属组

    chown  1.效用  更改一个或者多个文件或者目录的属主以及属组,使用职权范围是超等用户  2.格局  chown [选项] 用户或者组 文件  3.首要参量  --dereference:受影响 ...

  7. redhat 5的中文包安装

    中文包文件名.在iso文件的/server/文件夹下fonts-chinese-3.02-9.6.el5.noarch.rpmfonts-ISO8859-2-75dpi-1.0-17.1.noarch ...

  8. Hadoop 编写WordCount

    本文发表于本人博客. 前面几次讲了关于Hadoop的环境搭建.HDFS操作,今天接着继续.本来Hadoop源码中就有一个例子WordCount,但是今天我们来自己实现一个加深对这个Mapper.Red ...

  9. uva 1456

    这题说的是 给了 n 个 点 然后每个点 都有 相应的概率,你要将这n个点划分成w个集合使得 下面定义的这种算法 得到的 值最小 n1 是集合一的 个数  是 集合一内的每个点的概率和, 下面是分成两 ...

  10. IP地址与MAC地址

    作者:知乎用户链接:https://www.zhihu.com/question/21546408/answer/28155896来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...