Codeforces 483b friends and presents (Binary + number theory)

Source: Internet
Author: User

Link: codeforces 483b friends and presents

Question: 1 ~ The number of values in a straight direction is allocated to two sets. The first set requires the number of cnt1, the second one requires the number of cnt2, and the number of values in the first set.

It cannot be a multiple of X. Similarly, the second set cannot be a multiple of Y. The elements of the two sets cannot be the same.

Solution: This question is more difficult than the third question. I thought about it for a while. Second answer, V, and then judge.

When determining, you only need to determine whether the number of the Two sets is equal to or not. However, because some numbers can be divided into two sets, You need to determine the total number of allocable sets.

Whether or not the content is greater than cnt1 + cnt2, and the total allocable quantity needs to be used in the calculation.

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;ll gcd(ll a, ll b) {    return b == 0 ? a : gcd(b, a % b);}ll lcm(ll a, ll b) {    return a / gcd(a, b) * b;}ll c1, c2, x, y, v;bool judge(ll m, ll v) {    ll a = m - m / x;    ll b = m - m / y;    if (a < c1 || b < c2)        return false;    ll t = m - m / x - m / y + m / v;    if (a + b - t < c1 + c2)        return false;    return true;}int main () {    scanf("%lld%lld%lld%lld", &c1, &c2, &x, &y);    ll l = 1, r = 1e9 * 2, m;    v = lcm(x, y);    for (int i = 0; i < 100; i++) {        m = (l + r) >> 1;        if (judge(m, v))            r = m;        else            l = m;    }    printf("%lld\n", r);    return 0;}

Codeforces 483b friends and presents (Binary + number theory)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.