当有2^i个瓶子时,可以合成一个瓶子。
将n转换为二进制,有几个1就可以合成几个瓶子,当大于k时,就加上最低位的1的数(1100就加4),然后继续判断。
#include <iostream>
using namespace std;
typedef long long ll;
ll n, k, ans;
int setcount(ll x)
{
int ret = 0;
for (; x; x -= x & -x) ret++;
return ret;
}
int main(void)
{
cin >> n >> k;
while (setcount(n) > k) {
ans += n & -n;
n += n & -n;
}
cout << ans;
return 0;
}