题目链接:传送门

题目:

B. Friends and Presents
time limit per test
second
memory limit per test
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 , , ..., v. Of course you may choose not to present some numbers at all. A positive integer number greater than is called prime if it has no positive divisors other than and itself.
Input The only line contains four positive integers cnt1, cnt2, x, y ( ≤ cnt1, cnt2 < ; cnt1 + cnt2 ≤ ;  ≤ x < y ≤ ·) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
Output Print a single integer — the answer to the problem.
Examples
Input
Copy Output
Copy Input
Copy Output
Copy Note In the first sample you give the set of numbers {, , } to the first friend and the set of numbers {} to the second friend. Note that if you give set {, , } to the first friend, then we cannot give any of the numbers , , to the second friend. In the second sample you give the set of numbers {} to the first friend, and the set of numbers {, , } to the second friend. Thus, the answer to the problem is .

题目大意:

  已知素数x,y,要求从1开始分别分配cnt1,cnt2个数给x,y,且分配给x的数不能是x的倍数,分配给y的数不能是y的倍数。求所有分掉的数中的最大值的最小值。

  1 ≤ cnt1, cnt2 < 109; cnt1 + cnt2 ≤ 109; 2 ≤ x < y ≤ 3·104

思路:

  如果已知答案mid(滑稽):

  那么1-mid之间所有x的倍数不能分给x,那么优先分给y;

  同理:y的倍数都先分给x。当然lcm(x, y) = xy的倍数不能分,要减去这部分(容斥)。

  然后比较mid的没分配的部分,和cnt1,cnt2没分到的部分。

  这样可以用O(n)的时间验证答案,且答案是单调的,故用二分搞。

代码:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll; ll cnt1, cnt2, x, y; bool judge(ll mid) {
ll mul_of_x = mid/x;
ll mul_of_y = mid/y;
ll mul_of_xy = mid/x/y;
ll tmp = mid - mul_of_x - mul_of_y + mul_of_xy;
ll resx = max(cnt1 - mul_of_y + mul_of_xy, (ll));
ll resy = max(cnt2 - mul_of_x + mul_of_xy, (ll));
return resx + resy <= tmp;
} int main()
{
cin >> cnt1 >> cnt2 >> x >> y;
ll l = , r = 1e18;
ll ans = r;
while (l <= r) {
ll mid = (l+r) >> ;
if (judge(mid)) {
ans = min(ans, mid);
r = mid-;
}
else
l = mid+;
}
cout << ans << endl;
return ;
}

Codeforces483B. Friends and Presents(二分+容斥原理)的更多相关文章

  1. 【BZOJ 2440】 2440: [中山市选2011]完全平方数 (二分+容斥原理+莫比乌斯函数)

    2440: [中山市选2011]完全平方数 Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数 ...

  2. poj2773 —— 二分 + 容斥原理 + 唯一分解定理

    题目链接:http://poj.org/problem?id=2773 Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submi ...

  3. codeforces B. Friends and Presents(二分+容斥)

    题意:从1....v这些数中找到c1个数不能被x整除,c2个数不能被y整除! 并且这c1个数和这c2个数没有相同的!给定c1, c2, x, y, 求最小的v的值! 思路: 二分+容斥,二分找到v的值 ...

  4. Codeforces Round #275 (Div. 2) B. Friends and Presents 二分+数学

    8493833                 2014-10-31 08:41:26     njczy2010     B - Friends and Presents             G ...

  5. POJ2773(容斥原理)

    Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11458   Accepted: 4001 Descr ...

  6. Codeforces 483 - A/B/C/D/E - (Done)

    题目链接:http://codeforces.com/contest/483 A - Counterexample - [简单构造题] Your friend has recently learned ...

  7. 几个解决k染色问题的指数级做法

    几个解决k染色问题的指数级做法 ——以及CF908H题解 给你一张n个点的普通无向图,让你给每个点染上k种颜色中的一种,要求对于每条边,两个端点的颜色不能相同,问你是否存在一种可行方案,或是让你输出一 ...

  8. Educational Codeforces Round 37-G.List Of Integers题解

    一.题目 二.题目链接 http://codeforces.com/contest/920/problem/G 三.题意 给定一个$t$,表示有t次查询.每次查询给定一个$x$, $p$, $k$,需 ...

  9. 【BZOJ-2440】完全平方数 容斥原理 + 线性筛莫比乌斯反演函数 + 二分判定

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2371  Solved: 1143[Submit][Sta ...

随机推荐

  1. Module loader:模块加载器

    <p data-height="265" data-theme-id="0" data-slug-hash="XpqRmq" data ...

  2. day042 前端CSS选择器

    今日内容: 高级选择器 1.子类选择器 用 > 表示 类比于相对路径 选择的是前一级标签的子标签 2后代选择器 用空格表示 选择的是前一级标签的后代标签 3并集选择器 用,逗号表示 选择的是用逗 ...

  3. selenium(六)Headless Chrome/Firefox--PhantomJS停止支持后,使用无界面模式。

    简介: 以前都用PhantomJS来进行无界面模式的自动化测试,或者爬取某些动态页面. 但是最近selenium更新以后,'Selenium support for PhantomJS has bee ...

  4. Windows系统上设置 Git Bash 的 Font 及 Locale

    在windows 上使用 Git Bash 可以获得 unix 命令 操作体验. 但是初始的Git Bash的字体及语系都很不方便,需要自己设置. 在Git Bash的命令窗体上边框点击鼠标右键可以进 ...

  5. 使用perfect进行服务端开发

    最近闲来无事,研究了下基于perfect的swift后端开发.根据大神的博客进行了简单的配置,加深下印象也算是和各位分享一下. 参考博客:http://www.cnblogs.com/ludashi ...

  6. C++输出数组名

    1.如果C++输出的数组是char类型的,那么输出的就是数组中的元素. 2.如果使用的是其他类型的数组作为输出的话,那么就是一个16进制的地址. 3.还是那句话,对数组的操作,很多时候都是指针的操作, ...

  7. 总结小bug

    1.下拉刷新问题 //不要用scroll-view 他会阻止刷新 //改用view <template name="movieGridTemplate"> <!- ...

  8. python作业学员管理系统(第十二周)

    作业需求: 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下 讲师视图 管理班级,可创建班级,根据学员qq号把学员加入班级 可创建指定班级的上课纪录,注意一节上课纪录对应多条学 ...

  9. Linux学习 : Socket 网络编程入门

    一.socket()函数 int socket(int domain, int type, int protocol); domain:即协议域,又称为协议族(family).常用的协议族有,AF_I ...

  10. CountDownLatch在多线程程序中的应用

    一.CountDownLatch介绍 CountDownLatch是JDK1.5之后引入的,存在于java.util.concurrent包下,能够使一个线程等待其他线程完成动作后再执行.构造方法: ...