Neko Performs Cat Furrier TransformCodeForces - 1152B  

  题目大意:给你一个x,在40步操作以内把x变成2m−1,m为非负整数。对于每步操作,奇数步可以在(0<=n<=30)中挑选一个n,将x⊕(2n−1),而偶数步将x++。输出操作步数,以及在每个奇数步异或的n,多个答案,输出任一答案,保证至少有一个答案。

  一开始傻逼了,真的照题意所说的去写了一个深搜,果断超时了。其实每个奇数步也是异或一个二进制全1的数,那我们就直接把x尾部连续0部分,全部异或为1,那么在偶数步加1的话,又会往前进位填补前面的0(如果有的话0),然后再继续把x尾部连续0部分,全部异或为1,直到x为二进制全1即可。

  就比如x=80,二进制为1010000,我们第一步先异或个24-1=15即1111,x就变成了95=1011111,然后第二步x++,x就成了96=1100000,第三步在异或个25-1=31即11111,x变成了127=1111111,结束。

  因为1e6,最多就220左右,肯定不会超出40步。而取得末尾连续0部分正是树状数组里的那个x&(-x),不懂的可以去了解下。

 #include<cstdio>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
map<int,int> m;
vector<int> ans;
int main()
{
int cf2=;
//把终止情况,2^i-1标记一下
m[]=;
for(int i=;i<=;i++)
{
cf2<<=;
m[cf2-]=;
}
int x,step=;
scanf("%d",&x);
while(!m[x])
{
step++;
if(step&)
{
int y=x&(-x),n=;
x^=(y-);
while(y)
{
n++;
y>>=;
}
//y=2^(n-1)
ans.push_back(n-);
}
else
x++;
}
printf("%d\n",step);
for(int i=;i<ans.size();i++)
{
if(i)
putchar(' ');
printf("%d",ans[i]);
}
printf("\n");
return ;
}

慢慢来不急

Neko Performs Cat Furrier Transform CodeForces - 1152B 二进制思维题的更多相关文章

  1. Codeforces Round #554 (Div. 2) 1152B. Neko Performs Cat Furrier Transform

    学了这么久,来打一次CF看看自己学的怎么样吧 too young too simple 1152B. Neko Performs Cat Furrier Transform 题目链接:"ht ...

  2. Codeforces Round #554 (Div. 2) B. Neko Performs Cat Furrier Transform(思维题+log2求解二进制位数的小技巧)

    传送门 题意: 给出一个数x,有两个操作: ①:x ^= 2k-1; ②:x++; 每次操作都是从①开始,紧接着是② ①②操作循环进行,问经过多少步操作后,x可以变为2p-1的格式? 最多操作40次, ...

  3. Codeforces 1038D - Slime - [思维题][DP]

    题目链接:http://codeforces.com/problemset/problem/1038/D 题意: 给出 $n$ 个史莱姆,每个史莱姆有一个价值 $a[i]$,一个史莱姆可以吃掉相邻的史 ...

  4. CodeForces - 589D —(思维题)

    Welcoming autumn evening is the best for walking along the boulevard and npeople decided to do so. T ...

  5. CodeForces - 468A ——(思维题)

    Little X used to play a card game called "24 Game", but recently he has found it too easy. ...

  6. CodeForces - 669D——(思维题)

    Little Artem is fond of dancing. Most of all dances Artem likes rueda — Cuban dance that is danced b ...

  7. CodeForces - 631C ——(思维题)

    Each month Blake gets the report containing main economic indicators of the company "Blake Tech ...

  8. Sonya and Robots CodeForces - 1004C (思维题)

    Sonya and Robots time limit per test 1 second memory limit per test 256 megabytes input: standard in ...

  9. CodeForces - 603A-Alternative Thinking (思维题)

    Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO ...

随机推荐

  1. pat l2-14 列车调度 dilworth+nlog(n)最长上升子序列

    关于dilworth定理 这里引用一个大神的(http://blog.csdn.net/xuzengqiang/article/details/7266034) 偏序的概念: 设A是一个非空集,P是A ...

  2. inline元素、block元素

    inline元素 不会独占一行,相邻的行内元素会排列在同一行内,直到一行排不下才会换行 高.行高.以及外边距和内边距不可改变 宽度就是它的文字或图片的宽度,不可改变,随元素内容变化而变化 内联元素只能 ...

  3. Spingboot+Mybatis+Oracle项目配置

    配置过程参考: 项目创建:http://how2j.cn/k/springboot/springboot-eclipse/1640.html 集成Mybatis使用Oracle:https://www ...

  4. WebFont技术使用之如何在app中使用自定义字体

    参考 H5自定义字体解决方法(mark) 移动Web字体的使用 [原]移动web页面使用字体的思考 CSS @font-face规则 引用外部服务器字体

  5. python 列表反转

    反转: 将原列表反转,返回None: li = [1, 2, 3]li.reverse()print(li)# [3, 2, 1]1234不改变原列表,返回反转后的新列表: li = [1, 2, 3 ...

  6. java实现spark常用算子之TakeSample

    import org.apache.spark.SparkConf;import org.apache.spark.api.java.JavaRDD;import org.apache.spark.a ...

  7. Jquery 学习-菜鸟教程

    jquery效果和元素选择 //元素选择 $(this).hide(); $("p.test") //隐藏所有class="test"的<p>元素 ...

  8. 如何解决Win10系统更新显示0x80080300代码的错误?

    Win10系统自推出以来就不断的在完善更新,其越来越丰富的功能也吸引了越来越多的用户.好系统Win10系统:https://www.vkebao.com/os/index_2.html但最近有用户反映 ...

  9. 9、nginx常用基础模块

    1Nginx目录索引 ngx_http_autoindex_module模块处理以斜杠字符('/')结尾的请求(就是处理location /),并生成目录列表.当ngx_http_index_modu ...

  10. centos 7 Apache-Tomcat-8.5.46 安装 Web 应用服务器

    tomcat 官网版本地址:https://tomcat.apache.org/whichversion.html Servlet规格 JSP规范 EL规格 WebSocket规范 JASPIC规格 ...