You are given an integer k and an integer x. The price of a number num
is calculated by the count of set bits at positions x, 2x, 3x, etc., in
its binary representation, starting from the least significant bit. The
following table contains examples of how price is calculated.
x
num
Binary Representation
Price
1
13
00000** 1**** 1** 0** 1**
3
2
13
0 0 0 0 0** 1** 1 0 1
1
2
233
0** 1** 1** 1** 0** 1** 0 0 1
3
3
13
0 00 0 01** 1** 01
1
3
362
1 01** 1** 01 0 10
2
The accumulated price of num is the total price of numbers from
1 to num. num is considered cheap if its accumulated price is less
than or equal to k.
The price of a number is the count of set bits at positions x, 2x, 3x, ... in its binary representation. The accumulated price is the sum of prices from 1 to num. We want the largest num such that the accumulated price is at most k. Since the price function is monotonic, we can use binary search.
classSolution {
public:longlong price(int num, int x) {
longlong res =0;
for (int i = x -1; i <60; i += x) {
if (num & (1LL<< i)) res++;
}
return res;
}
longlongacc_price(int num, int x) {
longlong res =0;
for (int i =1; i <= num; ++i) res += price(i, x);
return res;
}
intmaximumNumber(longlong k, int x) {
int l =1, r =2e6, ans =0;
while (l <= r) {
int m = l + (r - l) /2;
if (acc_price(m, x) <= k) { ans = m; l = m +1; }
else r = m -1;
}
return ans;
}
};
classSolution {
privatelongprice(int num, int x) {
long res = 0;
for (int i = x - 1; i < 60; i += x) {
if (((num >> i) & 1) != 0) res++;
}
return res;
}
privatelongaccPrice(int num, int x) {
long res = 0;
for (int i = 1; i <= num; i++) res += price(i, x);
return res;
}
publicintmaximumNumber(long k, int x) {
int l = 1, r = 2000000, ans = 0;
while (l <= r) {
int m = l + (r - l) / 2;
if (accPrice(m, x) <= k) { ans = m; l = m + 1; }
else r = m - 1;
}
return ans;
}
}
classSolution {
privatefunprice(num: Int, x: Int): Long {
var res = 0Lvar i = x - 1while (i < 60) {
if ((num shr i) and 1!=0) res++ i += x
}
return res
}
privatefunaccPrice(num: Int, x: Int): Long {
var res = 0Lfor (i in1..num) res += price(i, x)
return res
}
funmaximumNumber(k: Long, x: Int): Int {
var l = 1var r = 2000000var ans = 0while (l <= r) {
val m = (l + r) / 2if (accPrice(m, x) <= k) {
ans = m
l = m + 1 } else {
r = m - 1 }
}
return ans
}
}
classSolution:
defprice(self, num: int, x: int) -> int:
res =0 i = x -1while i <60:
if (num >> i) &1:
res +=1 i += x
return res
defacc_price(self, num: int, x: int) -> int:
res =0for i in range(1, num +1):
res += self.price(i, x)
return res
defmaximumNumber(self, k: int, x: int) -> int:
l, r, ans =1, 2_000_000, 0while l <= r:
m = (l + r) //2if self.acc_price(m, x) <= k:
ans = m
l = m +1else:
r = m -1return ans
impl Solution {
pubfnmaximum_number(k: i64, x: i32) -> i32 {
fnprice(num: i32, x: i32) -> i64 {
letmut res =0;
letmut i = x -1;
while i <60 {
if (num >> i) &1!=0 {
res +=1;
}
i += x;
}
res
}
fnacc_price(num: i32, x: i32) -> i64 {
letmut res =0;
for i in1..=num {
res += price(i, x);
}
res
}
let (mut l, mut r, mut ans) = (1, 2_000_000, 0);
while l <= r {
let m = (l + r) /2;
if acc_price(m, x) <= k {
ans = m;
l = m +1;
} else {
r = m -1;
}
}
ans
}
}