Codeforces Round #554 (Div. 2)(B.位运算)

B. Neko Performs Cat Furrier Transform

题目链接:
http://codeforces.com/contest/1152/problem/B

Cat Furrier Transform is a popular algorithm among cat programmers to create longcats. As one of the greatest cat programmers ever exist, Neko wants to utilize this algorithm to create the perfect longcat.
Assume that we have a cat with a number x. A perfect longcat is a cat with a number equal 2m−1 for some non-negative integer m. For example, the numbers 0, 1, 3, 7, 15 and so on are suitable for the perfect longcats.
In the Cat Furrier Transform, the following operations can be performed on x:
(Operation A): you select any non-negative integer n and replace x with x⊕(2n−1), with ⊕ being a bitwise XOR operator.
(Operation B): replace x with x+1.
The first applied operation must be of type A, the second of type B, the third of type A again, and so on. Formally, if we number operations from one in the order they are executed, then odd-numbered operations must be of type A and the even-numbered operations must be of type B.
Neko wants to produce perfect longcats at industrial scale, thus for each cat Neko only wants to perform at most 40 operations. Can you help Neko writing a transformation plan?
Note that it is not required to minimize the number of operations. You just need to use no more than 40 operations.

Input

The only line contains a single integer x (1≤x≤106).

Output

The first line should contain a single integer t (0≤t≤40) — the number of operations to apply.

Then for each odd-numbered operation print the corresponding number ni in it. That is, print ⌈t2⌉ integers ni (0≤ni≤30), denoting the replacement x with x⊕(2ni−1) in the corresponding step.

If there are multiple possible answers, you can print any of them. It is possible to show, that there is at least one answer in the constraints of this problem.

Examples
input

39

output

4
5 3

input

1

output

0

input

7

output

0

Note

In the first test, one of the transforms might be as follows: 39→56→57→62→63. Or more precisely:

Pick n=5. x is transformed into 39⊕31, or 56.
Increase x by 1, changing its value to 57.
Pick n=3. x is transformed into 57⊕7, or 62.
Increase x by 1, changing its value to 63=26−1.
In the second and third test, the number already satisfies the goal requirement.

题目大意:

给你一个数,然后进行如下操作:

  • x^(2n-1)
  • x+1

第一次第一步,第二次第二步,如此类推
直至x满足x=2n-1;
输出步数,和取模的n

题目思路:

比赛的时候写了bfs结果爆内存了Orz,后来仔细想想,其实不难。
给你的每个数,你看成二进制,如
39
1 0 0 1 0 0 1
每次异或,都是相同的为0,不同的为1
所以我们可以从左边找起,每次遇到0,的时候,异或一次这个位置上的2n-1(1111),这样就不会影响上面那些已经改变或本来就是1的位置,然后判断,如果不是就继续执行。这样最后,我们就能把所有位置上的数变成1了。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll n[33];
ll cun[45];
int main(){
    ll j=1;
    n[0]=0;
    for(int i=1;i<=30;i++){
        j=j*2;
        n[i]=j-1;
    }
    ll x;
    cin>>x;
    int ed=0;
    for(int i=0;i<=30;i++){
        if(x==n[i]){
            printf("0\n");
            return 0;
        }
        if(x>n[i]&&x<n[i+1]){
            ed=i+1;
        }
    }
    //cout<<ed<<endl;
    int cnt=0,o=0,flag=0;
    while(ed!=0){
    if(cnt%2==1) {x=x+1;cnt++;}  //进行第二步
    else if(cnt%2==0)  //进行第一步
    for(int i=ed;i>=1;i--){
       if((1<<(i-1)&x)==0){  //判断位数上是不是0 
          cnt++;
          cun[o++]=i;
          ed=i-1;
          x=x^n[i];
          //cout<<i<<" "<<n[i]<<" "<<x<<endl;
          break;
            }
        ed=i;
        }
       for(int i=0;i<=30;i++){  //判断
        if(x==n[i]){
            flag=1;
            break;
        }
       }
       if(flag==1) break;
    }
    printf("%d\n",cnt);
    for(int i=0;i<o;i++){
        printf("%d ",cun[i]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值