Neko Performs Cat Furrier Transform CodeForces - 1152B 二进制思维题
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 二进制思维题的更多相关文章
- Codeforces Round #554 (Div. 2) 1152B. Neko Performs Cat Furrier Transform
学了这么久,来打一次CF看看自己学的怎么样吧 too young too simple 1152B. Neko Performs Cat Furrier Transform 题目链接:"ht ...
- Codeforces Round #554 (Div. 2) B. Neko Performs Cat Furrier Transform(思维题+log2求解二进制位数的小技巧)
传送门 题意: 给出一个数x,有两个操作: ①:x ^= 2k-1; ②:x++; 每次操作都是从①开始,紧接着是② ①②操作循环进行,问经过多少步操作后,x可以变为2p-1的格式? 最多操作40次, ...
- Codeforces 1038D - Slime - [思维题][DP]
题目链接:http://codeforces.com/problemset/problem/1038/D 题意: 给出 $n$ 个史莱姆,每个史莱姆有一个价值 $a[i]$,一个史莱姆可以吃掉相邻的史 ...
- CodeForces - 589D —(思维题)
Welcoming autumn evening is the best for walking along the boulevard and npeople decided to do so. T ...
- CodeForces - 468A ——(思维题)
Little X used to play a card game called "24 Game", but recently he has found it too easy. ...
- CodeForces - 669D——(思维题)
Little Artem is fond of dancing. Most of all dances Artem likes rueda — Cuban dance that is danced b ...
- CodeForces - 631C ——(思维题)
Each month Blake gets the report containing main economic indicators of the company "Blake Tech ...
- Sonya and Robots CodeForces - 1004C (思维题)
Sonya and Robots time limit per test 1 second memory limit per test 256 megabytes input: standard in ...
- CodeForces - 603A-Alternative Thinking (思维题)
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO ...
随机推荐
- navicat 使用 pymysql模块
新健库 ,新增字段+类型+约束 设计表:外键(自增) 新建查询 建立表模型 /* 数据导入: Navicat Premium Data Transfer Source Server : localho ...
- python设计购物车
设计购物车 一需求: 1.启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提 ...
- 01满包加记录最小路劲 L3-001. 凑零钱
过了这么久 正确理解01背包应该从记忆化搜索开始 这里对数字的取或者不取实际上就是一个01背包的模型 不过这里要求的是满包问题 那么我们动态便利的过程需要做一点 处理只有从0开始的能够向上更新 在就是 ...
- opencv 一些函数的耗时计算
Release 模式 -------------------------------------------------- smooth gaussian : 2 cvtColor CV_BGR2La ...
- The last packet successfully received from the server was 39,900 milliseconds ago问题解决
1,之前用Mysql或者mycat的时候都没有这个问题.后来改为haproxy+keepalived+mycat后出现这个问题 2,网上查了很多说法,我按照网上说的改了 datasource: url ...
- docker 安装ps命令
apt-get update && apt-get install procps
- MySql学习笔记【四、数据相关操作】
CURD--增改查删 创建数据 INSERT [INTO] tb_name [(col_name,...)] VALUES(val,..) 若列名缺省,表示插入全部列,也可指定部分列名 如: INSE ...
- go语言json转map
package util import ( "encoding/json" "fmt" ) // json转map函数,通用 func JSONToMap(st ...
- CentOS7磁盘空间不足,却找不到占用空间的大文件
1 df -ah 显示/根目录占用百分之九十 进入根目录对指定的文件夹查询容量 cd / du -sh * | sort -n 磁盘有50G,加起来有10G左右的文件找不到 2 自己影响中,已经清理 ...
- IoT 设备通信安全讨论
IoT 设备通信安全讨论 作者:360CERT 0x00 序言 IoT 设备日益增多的今天,以及智能家居这一话题愈发火热,智能家居市场正在飞速的壮大和发展,无数 IoT 设备正在从影片中不断的走向用户 ...