P9451 [ZSHOI-R1] 新概念报数 题解

Description

在此题中,对于一个数 x x x,若 popcount ( x ) ≥ 3 \texttt{popcount}(x)\geq3 popcount(x)3(即 x x x 在二进制下 1 \texttt{1} 1 的个数大于等于三时),那它是非法的,否则其为合法的。

给定 T T T 个数,如果当前的数 x x x 是非法的,则输出 No,Commander,否则输出第一个大于 x x x 的合法的数。

Solution

对于一个数 x x x,我们可以快速得出 popcount ( x ) \texttt{popcount}(x) popcount(x),若为非法,直接输出答案,否则还剩三种情况:

  1. popcount ( x ) = 0 \texttt{popcount}(x)=0 popcount(x)=0,说明 x = 0 x=0 x=0,输出 1 \texttt{1} 1 即可。
  2. popcount ( x ) = 1 \texttt{popcount}(x)=1 popcount(x)=1,输出 x + 1 x+1 x+1 即可,因为 popcount ( x + 1 ) = 2 \texttt{popcount}(x+1)=2 popcount(x+1)=2 popcount ( 1 + 1 ) = 1 \texttt{popcount}(1+1)=1 popcount(1+1)=1 为特例)
  3. popcount ( x ) = 2 \texttt{popcount}(x)=2 popcount(x)=2,设答案为 y y y,若 popcount ( y ) = 1 \texttt{popcount}(y)=1 popcount(y)=1 ,则 y = 2 ⌈ log ⁡ 2 x ⌉ + 1 y=2^{\left\lceil\log_2{x}\right\rceil+1} y=2log2x+1,比如 x = 10100 x=\texttt{10100} x=10100 y = 100000 y=100000 y=100000,而 popcount ( y ) = 2 \texttt{popcount}(y)=2 popcount(y)=2 时,则需要找到其从低到高第一个 1 1 1 的位置 k k k y = x + 2 k − 1 y=x+2^{k-1} y=x+2k1,因为在把第 k k k 位的 1 \texttt{1} 1 消掉时,能同时加 1 \texttt{1} 1 k + 1 k+1 k+1 位上,如 x = 10100 x=10100 x=10100 y = 11000 y=11000 y=11000,要比第一种更小,综上,输出 x + 2 k − 1 x+2^{k-1} x+2k1 即可。

Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int t;
int check(ll num){  // 得出popcount(x)
	int cnt=0;
	while(num){
		if(num&1){
			cnt++;
		}
		num>>=1;
	}
	return cnt;
}
ll next(ll x){ //记得开 long long
	if(check(x)==1||check(x)==0) return x+1;
	int cnt=0;
	ll y=x;
	while(x){
		if(x&1) break; //找到 k
		x/=2;
		cnt++;
	}
	ll xx=pow(2,cnt); // 加上 2^{k-1}
	return (ll)(y+xx);
}
int main(){
	cin>>t;
	while(t--){
		ll x;
		cin>>x;
		if(check(x)>=3) cout<<"No,Commander"<<endl;
		else cout<<next(x)<<endl;
	}
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对于洛谷上的p1036题目,我们可以使用Python来解决。下面是一个可能的解法: ```python def dfs(nums, target, selected_nums, index, k, sum): if k == 0 and sum == target: return 1 if index >= len(nums) or k <= 0 or sum > target: return 0 count = 0 for i in range(index, len(nums)): count += dfs(nums, target, selected_nums + [nums[i]], i + 1, k - 1, sum + nums[i]) return count if __name__ == "__main__": n, k = map(int, input().split()) nums = list(map(int, input().split())) target = int(input()) print(dfs(nums, target, [], 0, k, 0)) ``` 在这个解法中,我们使用了深度优先搜索(DFS)来找到满足要求的数列。通过递归的方式,我们遍历了所有可能的数字组合,并统计满足条件的个数。 首先,我们从给定的n和k分别表示数字个数和需要选取的数字个数。然后,我们输入n个数字,并将它们存储在一个列表nums中。接下来,我们输入目标值target。 在dfs函数中,我们通过迭代index来选择数字,并更选取的数字个数k和当前总和sum。如果k等于0且sum等于target,我们就找到了一个满足条件的组合,返回1。如果index超出了列表长度或者k小于等于0或者sum大于target,说明当前组合不满足要求,返回0。 在循环中,我们不断递归调用dfs函数,将选取的数字添加到selected_nums中,并将index和k更为下一轮递归所需的值。最终,我们返回所有满足条件的组合个数。 最后,我们在主程序中读入输入,并调用dfs函数,并输出结果。 这是一种可能的解法,但不一定是最优解。你可以根据题目要求和测试数据进行调试和优化。希望能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值