Luo Gu P1582 pour (hexadecimal number theory)

Title transfer

When there are 2 ^ i bottles, a bottle can be synthesized.
The n converted to binary, there are several several bottles 1 can be synthesized, when more than k, on the plus number (plus 4 to 1100) of 1 least significant bit, and then continues to determine.

#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;
}

Published 30 original articles · won praise 50 · views 5292

Guess you like

Origin blog.csdn.net/qq_43054573/article/details/104574091