跳到正文

题解

题解:P13830 【MX-X18-T2】「FAOI-R6」二进制与一 III(bit)

AC_Seed约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年8月24日
收录
2025年12月12日
更新
2026年8月29日
篇幅
约 2 分钟
题目
P13830

版本

  1. v1当前

Solution 【MX-X18-T2】

这题有很多档分,我们来分析一下。

  • 首先对于 2≤n≤102 \le n \le 10 的分,我们可以用搜索,如果 x1+x2+...+xk=nx_1+x_2+...+x_k=n 且 xi≥2x_i ≥ 2,则满足条件,计算每一个 xix_i 在二进制中 11 的个数,最后取 max⁡\max 即可。
  • 对于 nn 为 33 的倍数这一性质,我们需要想一下就可以知道。首先,我们把 33 转成二进制:(3)10=(11)2(3)_{10}=(11)_{2}。不难发现,二进制数里全是 11。那么我们可以将 nn 表示为 3k3k。那么答案就是 popcount(3)×kpopcount(3) \times k,即 2k2k,也就是 2×n32 \times \frac{n}{3}。
  • 基于上一档分的分析,我们还要再想一下就可以写出正解。对于 nn 而言,如果我们只考虑 33,那么有可能会有余数小于 22,无法拆分,会缩小答案。因此,我们需要考虑 (7)10=(111)2(7)_{10}=(111)_{2}。把这个数 nn 拆分成 7k+r7k+r。这样答案就是 popcount(7)×k+popcount(r),(r>1)popcount(7) \times k +popcount(r),(r>1),即 2×n7+popcount(r),(r>1)2 \times \frac{n}{7} +popcount(r),(r>1)。最后两者取最值,注意余数。在这里,rr 为余数。
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
int maxi=0;
int n;
int popcount(int x){
	int cnt=0;
	while(x){
		cnt+=(x%2==1);
		x/=2;
	}
	return cnt;
} 
void work(){
	cin>>n;
	if(n>7){
		int tt=popcount(7)*(n/7);
        int tt2=popcount(3)*(n/3);
        if(n%7>=2) tt+=popcount(n%7);
        if(n%3>=2) tt2+=popcount(n%3);
		cout<<max(tt,tt2)<<"\n";
		return ;
	}
	else{
		int tt2=popcount(3)*(n/3);
        if(n%3>=2) tt2+=popcount(n%3);
		cout<<tt2<<"\n";
	}
	return ;
}
signed main(){
	int t;
	cin>>t;
	while(t--){
		work();
	}
	return 0;
}
原文