我是靠谱客的博主 清秀战斗机,最近开发中收集的这篇文章主要介绍Codeforces1152B 模拟 位运算 & ^,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

https://codeforces.com/contest/1152/problem/B
&找到最高0位 异或值一定最大
&是1 1&1 异或值为0
10000…&运算 1&0为0 找到最高0位与1异或 一定变大的快

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
ll gcd(ll a,ll b)
{
return b!=0?gcd(b,a%b):a;
}
vector<ll> ans;
int main()
{
IO;
ll x;
cin>>x;
ll cnt=0;
for(int i=0;i<=20;i++)
{
if(x>pow(2,i)-1)
{
continue;
}
else
{
cnt=i;
break;
}
}
ll res=pow(2,cnt)-1;
cnt--;
//	cout<<cnt<<endl;
//	cout<<res<<endl;
if(res==x)
{
cout<<0<<endl;
return 0;
}
ll c=1,num=0;
while(x!=res)
{
if(c&1)
{
num=0;
for(int i=1<<cnt;x&i;i=i/2)
{
cnt--;
}
cnt++;
ans.push_back(cnt);
num=pow(2,cnt)-1;
x=x^num;
}
else
x++;
c++;
}
cout<<c-1<<endl;
for(auto k:ans)
{
cout<<k<<" ";
}
return 0;
}
/*
3
0 2 1
*/

最后

以上就是清秀战斗机为你收集整理的Codeforces1152B 模拟 位运算 & ^的全部内容,希望文章能够帮你解决Codeforces1152B 模拟 位运算 & ^所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部