跳到正文

题解

P13830 二进制与一 III 题解

ARIS0_0约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟
题目
P13830

版本

  1. v1当前

1. 题意解释

对于一个数 nn,你需要将其分解为若干个大于 22 的数 aia_i 的和,且使得 ∑popcount⁡(ai)\sum \operatorname{popcount}(a_i) 最大,求 ∑popcount⁡(ai)\sum \operatorname{popcount}(a_i) 的最大值。

2. 思路

不难想到贪心。

我们令 f(x)=popcount⁡(x)xf(x)=\dfrac{\operatorname{popcount}(x)}{x},不难想到,我们只需要尽可能地把 nn 分解为使得 f(x)f(x) 最大的那个 xx,即可使答案达到最值。

至于如何找到这个最值 xx,我们可以采用打表。

打表程序如下:

C++
#include<bits/stdc++.h>
using namespace std;
int t,n;
int f(int x){
	int cnt=0;
	while(x){
		cnt+=x%2;
		x/=2;
	}
	return cnt;
}
int main(){
	for(int i=2;i<=100;i++){
		cout<<1.0*f(i)/i<<endl;
	}
	return 0;
}

输出不放了,我们不难发现 f(3)f(3) 为最大,其次为 f(2)f(2)。

于是思路如下:将 nn 分解为 3a+2b3a+2b 的形式,其中 0≤b≤20\le b\le2。

于是按照 nn 对 33 取模的结果分类讨论即可。

代码不放了。

对于 f(3)f(3) 为最值的严谨证明放在这里了,推荐有一定数学基础的同学食用。

原文