publicintnthMagicalNumber(int n, int a, int b) { // 最大公倍数 intc= a * b / gcd(a, b); longleft=0, right = (long) Math.min(a, b) * n; while (left < right) { longmid= left + right >> 1; // 容斥原理 if (mid / a + mid / b - mid / c >= n) right = mid; else left = mid + 1; } return (int) (right % MOD); }
privateintgcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } }