const MOD = 10 ** 9 + 7; var nthMagicalNumber = function (n, a, b) { if (a > b) { return nthMagicalNumber(n, b, a); } if (b % a == 0) { return (a * n) % MOD; } // 求出a,b的最小公倍数以及中间的二者的倍数 const res = commonMultiple(a, b); console.log(res); const len = res.length; let last = res[len - 1]; const k = Math.floor((n - 1) / len); const rest = (n - 1) % len; return (last * k + res[rest]) % MOD; };
var commonMultiple = function (a, b) { const ans = []; let proda = a; let prodb = b; while (proda !== prodb) { if (proda < prodb) { ans.push(proda); proda += a; } elseif (prodb < proda) { ans.push(prodb); prodb += b; } } ans.push(proda); return ans; };