洛谷 P1582 倒水(进制 数论)

题目传送

当有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;
}

发布了30 篇原创文章 · 获赞 50 · 访问量 5292

猜你喜欢

转载自blog.csdn.net/qq_43054573/article/details/104574091