Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Numerics;
- public static class solver
- {
- static long Gcd(long a, long b)
- {
- while (b != 0) {
- var rem = a % b;
- a = b;
- b = rem;
- }
- return a;
- }
- static long cnt1, cnt2, x, y;
- static long CountMatching(long max, long divisor)
- {
- return max / divisor;
- }
- static bool Possible(long max)
- {
- var notFor1 = CountMatching(max, x);
- var notFor2 = CountMatching(max, y);
- var notFor12 = CountMatching(max, x * y);
- var for12 = max - CountMatching(max, Gcd(x, y));
- var left = (max - notFor1) + (max - notFor2) - for12;
- return left >= (cnt1 + cnt2);
- }
- public static void Main()
- {
- var input = Console.ReadLine().Split(null);
- cnt1 = long.Parse(input[0]);
- cnt2 = long.Parse(input[1]);
- x = long.Parse(input[2]);
- y = long.Parse(input[3]);
- var l = 0L;
- var r = long.MaxValue / 2;
- while (l < r - 1) {
- var m = (l + r) / 2;
- if (Possible(m))
- r = m;
- else
- l = m;
- }
- if (Possible(l))
- Console.WriteLine(l);
- else
- Console.WriteLine(r);
- #if !ONLINE_JUDGE
- Console.ReadLine();
- #endif
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement