class Solution {
public int numWaterBottles(int numBottles, int numExchange) {
int ans = numBottles;
int left = 0;
while (numBottles >= numExchange){
left = numBottles%numExchange;
numBottles /= numExchange;
ans += numBottles;
numBottles += left;
}
return ans;
}
}
class Solution {
int mod = 1337;
public int superPow(int a, int[] b) {
int ans = 1;
for (int i = b.length - 1; i >= 0; i--) {
ans *= pow(a,b[i]);
ans %= mod;
a = pow(a,10);
}
return ans;
}
public int pow(int a, int b){
if (b==0) return 1;
if (b==1) return a%mod;
int ans = pow(a,b/2);
return ((ans * ans)%mod) * pow(a,b%2) % mod;
}
}
class Solution {
public int countOrders(int n) {
long ans = 1;
long mod = (long) (1e9+7);
for (int i = 2; i <= n; i++) {
ans *= (2 * i * i - i);
ans %= mod;
}
return (int)ans;
}
}
class Solution {
public int numIdenticalPairs(int[] nums) {
int[] arr = new int[101];
int ans = 0;
for (int num : nums) {
arr[num]++;
ans += arr[num]-1;
}
return ans;
}
}
class Solution {
public int reachNumber(int target) {
if (target==0)return 0;
target = Math.abs(target);
int k = (int) Math.sqrt(2 * target);
k--;
while ((1 + ++k) * k / 2 < target){}
int d = (1 + k) * k / 2 - target;
while (d%2!=0) {
d += ++k;
}
return k;
}
}
class Solution {
public int nthMagicalNumber(int n, int a, int b) {
long mod = (long)(1e9 + 7);
long l = 2;
long r = (long) n * Math.min(a,b);
while (l < r){
long mid = l + ((r-l)>>1);
if(f(a,b,mid) < n){
l = mid + 1;
}else{
r = mid;
}
}
return (int)(l % mod);
}
long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
long f(long a, long b, long x) {
return (x / a) + (x / b) - (x / lcm(a, b));
}
}