Codeforces Round #554 (Div. 2) B. Neko Performs Cat Furrier Transform (二进制的位运算的设计与思考)

B. Neko Performs Cat Furrier Transform

http://codeforces.com/contest/1152/problem/B

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 xx. A perfect longcat is a cat with a number equal 2m−12m−1 for some non-negative integer mm. For example, the numbers 00, 11, 33, 77, 1515 and so on are suitable for the perfect longcats.

In the Cat Furrier Transform, the following operations can be performed on xx:

  • (Operation A): you select any non-negative integer nn and replace xx with x⊕(2n−1)x⊕(2n−1), with ⊕⊕ being a bitwise XOR operator.
  • (Operation B): replace xx with x+1x+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 4040 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 4040 operations.

Input

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

Output

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

Then for each odd-numbered operation print the corresponding number nini in it. That is, print ⌈t2⌉⌈t2⌉ integers nini (0≤ni≤300≤ni≤30), denoting the replacement xx with x⊕(2ni−1)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

Copy

39

output

Copy

4
5 3 

input

Copy

1

output

Copy

0

input

Copy

7

output

Copy

0

Note

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

  1. Pick n=5n=5. xx is transformed into 39⊕3139⊕31, or 5656.
  2. Increase xx by 11, changing its value to 5757.
  3. Pick n=3n=3. xx is transformed into 57⊕757⊕7, or 6262.
  4. Increase xx by 11, changing its value to 63=26−163=26−1.

In the second and third test, the number already satisfies the goal requirement.

核心思想:寻找n值

39    ‭00100111‬

31    ‭00011111‬ 为  00100000减1;为2的5次方,n值5

56    ‭00111000‬

#include <bits/stdc++.h>
#include<windows.h>
#define ll long long int
#define mod 1000000007
//#define pii pair<int, int>
//#define fr(n) for (int i = 0; i < n; i++)
//#define fr1(n) for (int i = 1; i <= n; i++)
using namespace std;
vector<int> vi;

int x;
bool judge(int num)
{
    for( int i=0; i <= 21; i++)
    {
        if(   ( num &( 1<<i ) )==false  &&  (num >= ( 1<<i ) ) )
        {
            return false;
        }

    }
    return true;
}

int change(int num)
{
    //bool w = 0;
    for (int i = 21; i >= 0; i--)//2的20次方为1048576,不会超过20位
    {
        /*if ( ! (x & (1 << i)) )
            {
                if (w)
                {
                    vi.push_back(i + 1);
                    x ^= ((1 << (i + 1)) - 1);
                    break;
                }
            }
            else
                w = 1;
        //if (  (num & (1 << i) )==true && (num & (1 << (i-1) ) )==false )*/
        if (  (num & (1 << i) ) && !(num & (1 << (i-1) ) ) )
        {

            vi.push_back( i );
            num ^= ( (1 << i ) - 1);
            return num;
        }
    }
}

int main()
{
    int v = 0;//程序进行40次,奇数和偶数的集合各一半,所以只进位20次
    cin >> x;
    while (1)
    {
        if ( judge(x) )
        {
            break;
        }

        v++;
        x=change(x);
        if ( judge(x) )
        {
            break;
        }

        v++;
        x++;

    }

    cout << v << '\n';

    vector<int>::iterator it=vi.begin();
    for (; it!=vi.end(); it++)
        cout << *it << ' ';


}

猜你喜欢

转载自blog.csdn.net/while_black/article/details/89531805
今日推荐