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. http请求之of_ordering_getmiditem

    //Public function of_ordering_getmiditem (string as_instr,string as_key) returns string //string as_ ...

  2. 安装calico

    安装docker:https://www.cnblogs.com/cjsblogs/p/8717304.html 安装etcd集群:https://www.cnblogs.com/cjsblogs/p ...

  3. Nmap 常用命令语法

    Nmap是一个网络连接端扫描软件,用来扫描网上电脑开放的网络连接端,确定哪些服务运行在哪些连接端,并且推断计算机运行哪个操作系统,正如大多数被用于网络安全的工具,Nmap也是不少黑客及骇客爱用的工具, ...

  4. Java 封装与类

    一.面向对象编程 面向对象编程三大特性:封装.继承和多态. 类是实现封装的手段,是面向对象编程的基本单元. 封装隐藏了类的内部实现细节,暴露给外界可控的操作,提高数据的完整性和安全性,提高模块的可重用 ...

  5. 【原创】大叔经验分享(67)spring boot启动报错

    spring boot 启动报错: Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback Logg ...

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

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

  7. O039、Unshelve Instance 操作详解

    参考https://www.cnblogs.com/CloudMan6/p/5529915.html   上一节我们 shelve Instance 到 Glance,本节学习如何通过 unshelv ...

  8. scrapy操作指南

    Scrapy安装:(scrapy依赖包过多推荐使用下面的方法) 先安装Anaconda,然后 运行conda install Scrapy 创建scrapy项目: 1,scrapy startproj ...

  9. python 列表字典按照字典中某个valu属性进行排序

    对用户名进行排序 1. 直接上代码 base_dn_list = [ {', 'tenant': 'HAD', 'role': {'roleID': 'project', 'roleName': '项 ...

  10. Odoo的 数据添加修改删除代码和对应的方式

    完整的可用命令如下: (0, _ , {‘field’: value})新建一条记录并将其与之关联 (1, id, {‘field’: value})更新已关联记录的值 (2, id, _)移除关联并 ...