C - Submask

You are given a non-negative integer NN. Print all non-negative integers xx that satisfy the following condition in ascending order.

  • The set of the digit positions containing 11 in the binary representation of xx is a subset of the set of the digit positions containing 11 in the binary representation of NN.
    • That is, the following holds for every non-negative integer kk: if the digit in the "2^k2ks" place of xx is 11, the digit in the 2^k2ks place of NN is 11

题意:给你一个数n,他的二进制表示的所有1的位数的集合是s ,求满足二进制表示的所有的1的位数的集合是s的子集的数

思路:先把每一位1放进一个数组里,下标想从0开始,看看有几位,然后状态压缩,转换成二进制一个一个枚举每一位选或不选的状态

比如:有4位,那么我们就令所有状态为op=1<<4,然后从0到op-1枚举i,看i的每一位是不是1,如果是1的话就说明选了哪一位,最后看所有选的数加起来是多少就行

/*

 .----------------.  .----------------.  .----------------.  .----------------. 
| .--------------. || .--------------. || .--------------. || .--------------. |
| |  ________    | || |  _________   | || | ____    ____ | || |     ____     | |
| | |_   ___ `.  | || | |_   ___  |  | || ||_   \  /   _|| || |   .'    `.   | |
| |   | |   `. \ | || |   | |_  \_|  | || |  |   \/   |  | || |  /  .--.  \  | |
| |   | |    | | | || |   |  _|  _   | || |  | |\  /| |  | || |  | |    | |  | |
| |  _| |___.' / | || |  _| |___/ |  | || | _| |_\/_| |_ | || |  \  `--'  /  | |
| | |________.'  | || | |_________|  | || ||_____||_____|| || |   `.____.'   | |
| |              | || |              | || |              | || |              | |
| '--------------' || '--------------' || '--------------' || '--------------' |
 '----------------'  '----------------'  '----------------'  '----------------'

*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<deque>
#include<cmath>
#include<stack>
#define int long long
#define lowbit(x) x&(-x)
#define PI 3.1415926535
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int gcd(int a,int b){
	return b>0 ? gcd(b,a%b):a;
}
/*
int dx[8]={-2,-2,-1,1,2,2,-1,1};
int dy[8]={-1,1,2,2,1,-1,-2,-2};
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
int dx[8]={-1,1,0,0,-1,-1,1,1};
int dy[8]={0,0,-1,1,-1,1,-1,1};
*/
//int e[N],ne[N],h[N],idx,w[N];
/*void add(int a,int b,int c){
	e[idx]=b;
	w[idx]=c;
	ne[idx]=h[a];
	h[a]=idx++;
}
*/
const int N=2e5+10;
int n;
int a[N];
int f[65];
void init(){
	f[0]=1;
	for(int i=1;i<=60;i++){
		f[i]=f[i-1]*2;
	}
}
void sove(){
	cin>>n;
	int k=0,con=0;
	while(n){
		if(n&1)a[con++]=f[k];
		k++;
		n>>=1;
	}
//	cout<<con<<endl;
	int num=1<<con;
//	cout<<num<<endl;
	for(int i=0;i<num;i++){
		int x=i,cnt=0;
		int ans=0;
		while(x){
			if(x&1)ans+=a[cnt];
			cnt++;
			x>>=1;
		}
		cout<<ans<<endl;
	}
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie() ,cout.tie() ;
	int t=1;
	init();
//	cin>>t;
	while(t--){
		sove();
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值