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

题目链接:传送门

题目:

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, x, y (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 x, y are prime.
Output

Print a single integer — the answer to the problem.
Examples
Input
Copy

3 1 2 3

Output
Copy

5

Input
Copy

1 3 2 3

Output
Copy

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.

题目大意:

  已知素数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)0);
    ll resy = max(cnt2 - mul_of_x + mul_of_xy, (ll)0);
    return resx + resy <= tmp;
}

int main()
{
    cin >> cnt1 >> cnt2 >> x >> y;
    ll l = 0, r = 1e18;
    ll ans = r;
    while (l <= r) {
        ll mid = (l+r) >> 1;
        if (judge(mid)) {
            ans = min(ans, mid);
            r = mid-1;
        }
        else
            l = mid+1;
    }
    cout << ans << endl;
    return 0;
}

原文地址:https://www.cnblogs.com/Lubixiaosi-Zhaocao/p/9951673.html

时间: 2024-11-05 19:37:19

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

[二分+容斥原理] poj 2773 Happy 2006

题目链接: http://poj.org/problem?id=2773 Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9131   Accepted: 3073 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1.

Codeforces 483B - Friends and Presents - [二分]

题目链接:http://codeforces.com/contest/483 A - Counterexample - [简单构造题] Your friend has recently learned about coprime numbers. A pair of numbers $(a,?b)$ is called coprime if the maximum number that divides both $a$ and $b$ is equal to one. Your friend

hdu3388Coprime 二分+容斥原理

//找第k个和n,m互质的数 //由容斥原理可得 //在[1,x]范围内且与n不互质的数的个数为: //对于所有的n的素数因子:和一个素数因子不互质的个数-两个素数因子相乘的个数+三个素数因子相乘的个数-..... //对于x越大,在[1 , x]范围内的与n,m互质的数越多,所以存在单调性,可以用二分找到刚好有k个数和n,m互质 #include<cstdio> #include<cstring> #include<iostream> #include<map&

Codeforces 483B Friends and Presents(二分+数论)

题目链接:Codeforces 483B Friends and Presents 题目大意:要将1~v直间的数分配到两个集合中,第一个集合需要cnt1个数,第二个需要cnt2个数,第一个集合中的数 不能是x的倍数,同理第二个集合不能是y的倍数,两集合元素不能相同,问说v最小可以为多少. 解题思路:这题比第三题要难,想了有一会.二分答案,v,然后判断. 判断的时候只要分别判断集合一,二个数是否满足,但是因为有些数可以被分到两个集合,所以要判断总的可分配个数 是否满足大于cnt1+cnt2,计算总

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

题意:从1....v这些数中找到c1个数不能被x整除,c2个数不能被y整除! 并且这c1个数和这c2个数没有相同的!给定c1, c2, x, y, 求最小的v的值! 思路: 二分+容斥,二分找到v的值,那么s1 = v/x是能被x整除的个数 s2 = v/y是能被y整除数的个数,s3 = v/lcm(x, y)是能被x,y的最小公倍数 整除的个数! 那么 v-s1>=c1 && v-s2>=c2 && v-s3>=c1+c2就是二分的条件! 1 #incl

ubuntu,从新建一个用户,到转到新建用户的命令行操作

题目链接: http://poj.org/problem?id=2773 Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9131   Accepted: 3073 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1.

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

几个解决k染色问题的指数级做法 ——以及CF908H题解 给你一张n个点的普通无向图,让你给每个点染上k种颜色中的一种,要求对于每条边,两个端点的颜色不能相同,问你是否存在一种可行方案,或是让你输出一种可行方案,或是让你求出满足条件的最小的k.这种问题叫做k染色问题.众所周知,当k>2时,k染色问题是NP的.但是相比$O(k^n)$的暴力搜索来说,人们还是找到了很多复杂度比较优越的指数级做法.本文简单介绍其中几种. 因为对于$O(n^22^n)$来说,$O(n^2)$小得可以忽略不计,所以在本文

Codeforces483B——二分——Friends and Presents

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 tha

POJ 2773 Happy 2006#素数筛选+容斥原理+二分

http://poj.org/problem?id=2773 说实话这道题..一点都不Happy好吗 似乎还可以用欧拉函数来解这道题,但正好刚学了容斥原理和二分,就用这个解法吧. 题解:要求输出[1,m]中与m互质的第k个数,先打表,找到m的所有质因数,然后用二分实现,最开始区间为[1,2^60],利用容斥原理去找区间[1,mid]内素数的个数t,不断进行二分,直到所查找的区间[l,r]内素数的个数t等于k,mid=l=r,则此时的l就是第k个与m互质的数. #include<iostream>