878. 第 N 个神奇数字

https://leetcode-cn.com/problems/nth-magical-number/

class Solution {
public:
int gcd(int x, int y) { return x==0? y:gcd(y % x, x);}
   
    int nthMagicalNumber(int N, int A, int B) {
        int MOD = 1e9 + 7;
        int L = A / gcd(A, B) * B;

        long lo = 0;
        long hi = (long) 1e15;
        while (lo < hi) {
            long mid = lo + (hi - lo) / 2;
            if (mid / A + mid / B - mid / L < N)
                lo = mid + 1;
            else
                hi = mid;
        }

        return (int) (lo % MOD);
    }
}; 
posted @ 2020-03-26 21:27  aaaaassss  阅读(170)  评论(0编辑  收藏  举报