CF 483B. Friends and Presents 数学 (二分) 难度:1
1 second
256 megabytes
standard input
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.
The only line contains four positive integers cnt1, cnt2, x, y (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 x, y are prime.
Print a single integer — the answer to the problem.
3 1 2 3
5
1 3 2 3
4
#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的更多相关文章
- Codeforces 483B - Friends and Presents(二分+容斥)
483B - Friends and Presents 思路:这个博客写的不错:http://www.cnblogs.com/windysai/p/4058235.html 代码: #include& ...
- 快速切题CF 158B taxi 构造 && 82A double cola 数学观察 难度:0
实在太冷了今天 taxi :错误原因1 忽略了 1 1 1 1 和 1 2 1 这种情况,直接认为最多两组一车了 2 语句顺序错 double cola: 忘了减去n的序号1,即n-- B. Taxi ...
- CodeForces 483B Friends and Presents
Friends and Presents Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I ...
- Codeforces483B. Friends and Presents(二分+容斥原理)
题目链接:传送门 题目: B. Friends and Presents time limit per test second memory limit per test megabytes inpu ...
- UVA 11881 Internal Rate of Return(数学+二分)
In finance, Internal Rate of Return (IRR) is the discount rate of an investment when NPV equals zero ...
- HDU 6216 A Cubic number and A Cubic Number(数学/二分查找)
题意: 给定一个素数p(p <= 1e12),问是否存在一对立方差等于p. 分析: 根据平方差公式: 因为p是一个素数, 所以只能拆分成 1*p, 所以 a-b = 1. 然后代入a = b + ...
- codeforces 483B Friends and Presents 解题报告
题目链接:http://codeforces.com/problemset/problem/483/B 题目意思:有两个 friends,需要将 cnt1 个不能整除 x 的数分给第一个friend, ...
- UVA 10668 - Expanding Rods(数学+二分)
UVA 10668 - Expanding Rods 题目链接 题意:给定一个铁棒,如图中加热会变成一段圆弧,长度为L′=(1+nc)l,问这时和原来位置的高度之差 思路:画一下图能够非常easy推出 ...
- CF 551E. GukiZ and GukiZiana [分块 二分]
GukiZ and GukiZiana 题意: 区间加 给出$y$查询$a_i=a_j=y$的$j-i$最大值 一开始以为和论文CC题一样...然后发现他带修改并且是给定了值 这样就更简单了.... ...
随机推荐
- mysql 数据操作 单表查询 group by 聚合函数
强调: 如果我们用unique的字段作为分组的依据,则每一条记录自成一组,这种分组没有意义 多条记录之间的某个字段值相同,该字段通常用来作为分组的依据 如果按照每个字段都是唯一的进行分组,意味着按照这 ...
- Django之logging日志使用
Logger模块 是python中用于便捷记录日志且线程安全的模块 使用logging模块记录日志涉及四个主要类: logger提供了应用程序可以直接使用的接口: handler将(logger创建的 ...
- Kafka丢失数据问题优化总结
数据丢失是一件非常严重的事情事,针对数据丢失的问题我们需要有明确的思路来确定问题所在,针对这段时间的总结,我个人面对kafka 数据丢失问题的解决思路如下: 是否真正的存在数据丢失问题,比如有很多时候 ...
- POJ3233:Matrix Power Series(矩阵快速幂+二分)
http://poj.org/problem?id=3233 题目大意:给定矩阵A,求A + A^2 + A^3 + … + A^k的结果(两个矩阵相加就是对应位置分别相加).输出的数据mod m.k ...
- Deep Learning(3)算法简介
查看最新论文 Yoshua Bengio, Learning Deep Architectures for AI, Foundations and Trends in Machine Learning ...
- Java小项目迷你图书管理系统
package 迷你图书管理系统; import java.util.Scanner; public class BookMgr { public static void main(String[] ...
- php composer使用过程
1.安装composer curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/compos ...
- Digital Image Processing 学习笔记1
第一章 1.1 数字图像 一幅图像可以定义为一个而为函数, 其中x和y是空间坐标,而在任何一对空间坐标(x, y)处的幅值f称为图像在该点处的强度或灰度.当x, y和灰度值f是有限的离散数值时,该图像 ...
- python中小数点后取2位(四舍五入)以及取2位(四舍五不入)
一.小数点后取2位(四舍五入)的方法方法一:round()函数其实这个方法不推荐大家使用,查询资料发现里面的坑其实很多,python2和python3里面的坑还不太一样,在此简单描述一下python3 ...
- c++第十八天
p105~p108: 1.迭代器与数组的不同:迭代器的索引值必须是无符号类型,数组的索引无此要求. 2.使用数组的时候编译器一般会把它转化为指针. 3.auto ia2(数组名);的结果是:获得一个指 ...