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)