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. 界面(UI)测试基础

    界面测试,在web程序雏形后就持续测试到项目交付. 项目中的界面测试 此刻需要感慨下“产品经理”的重要性:没有产品把关整个项目,做出来的东西只能叫做“程序”而非“产品” 有产品的工作流,测试在界面测试 ...

  2. tensorflow零起点快速入门(5) --强化学习摘录截图

    tf.random_normal_initializer tf的GraphKeys用法 tf.reduce_mean tf.squared_difference 非tf中的zip,python的zip ...

  3. Java Serializable Objects(序列化)

    https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html 序列化一个对象,意味着把这个对象的状态转化为字节流,而且个字节流 ...

  4. 【原创】Linux基础之logrotate

    logrotate logrotate ‐ rotates, compresses, and mails system logs logrotate is designed to ease admin ...

  5. O052、Create Volume 操作 (Part III)

    参考https://www.cnblogs.com/CloudMan6/p/5617980.html       Jun 20 17:15:56 DevStack-Rocky-Compute-22 c ...

  6. httpclient 多附件上传

    多附件上传实例: /** * 多附件上传 * @param host * @param uri * @param attachment 附件 * @param param body参数 * @retu ...

  7. 正着打星星(js)

    //让用户输入行数,使用for循环嵌套打出正着的星星来,行数等于用户输入的数字 //例如:用户输入6 // * // *** // ***** // ******* // ********* // * ...

  8. 无聊开始玩路由器,入门Tomato固件

    无聊开始玩路由器,入门Tomato固件 在Wordpress上消失了一段时间,其实我最近几个月都比较宅.相信最近大家都了解过新出的一款很NB的路由器叫极路由,功能很强大,8G的可存储空间,可以安装路由 ...

  9. Arm汇编指令集2

    什么是协处理器: SoC内部另一处理核心,协助CPU实现某些功能,被主CPU调用执行一定任务. ARM设计上支持16个协处理器,但是一般SoC只实现其中的CP15(cp就是cooperation pr ...

  10. shell脚本基础和grep文本处理工具企业应用3

    文本处理工具:    linux上文本处理三剑客        grep,egrep,fgrep:文本过滤工具(模式:pattern)工具            grep:默认支持的是基本正则表达式: ...