Codeforces Round #554 (Div. 2) Neko Performs Cat Furrier Transform 异或

B. Neko Performs Cat Furrier Transform

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.

每次碰到异或都碰壁,还是第一次做出来了,有点惊喜,写篇论文记录一下。

思想:

首先发现变化的终点,比如39在32和64之间,那么变化的终点就是64.

 2^n-1化成二进制就是全是1,那每次异或操作的时候就是用x^111...,

比如18化成二进制:

10010 用它去异或1111就会把他往64靠近

得到11101,加一得11110,再去异或1即可得到32.

那么关键就是去找x化成二进制之后,从前到后第一个0出现的位置。得到用于去异或x的y。

凌乱代码。


#pragma warning(disable:4996)
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"math.h"
char a[105];
int b[50];
int main()
{
	int x;
	while (~scanf("%d", &x))
	{
		int ans;
		int m = 0,v=0;
		itoa(x, a, 2);
		int ll = strlen(a);
		while (1)
		{
			ans = 1;
			itoa(x, a, 2);
			int l = strlen(a);
			int flag = 0;
			//printf("!!!!!!!!   %s\n", a);
			for (int i = 0; i < l; i++)
			{
				if (a[i] != '1')
				{
					flag = i;
					break;
				}
				if (i == l - 1)
				{
					ans = 0;
				}
			}
			if (ans == 0)
			{
				break;
			}
			double h = l - flag;
			int y = pow(2.0, h) - 1;
			b[m++] = h;
		//	printf("~~~~~~~~~%d  %d\n", x ^ y, int(pow(2.0, ll) - 1));
			if ((x ^ y) == int(pow(2.0, ll) - 1))
			{
			//	printf("111111111111\n");
				break;
			}
			x = (x ^ y) + 1;
			v++;
			if (x == pow(2.0, double(ll)) - 1)
			{
				break;
			}
		}
		printf("%d\n", m + v);
		for (int i = 0; i < m; i++)
		{
			printf("%d ", b[i]);
		}
		if (ans)
		{
			printf("\n");
		}
		
	}
	return 0;
}
/*
18
16 + 2 
10010
01111

11101

11110
00001

11111

39
32 + 0 + 0 + 4+2+1
100111
011111
111000
32+16+8
56




*/
发布了58 篇原创文章 · 获赞 20 · 访问量 5229

猜你喜欢

转载自blog.csdn.net/qq_41658124/article/details/89574626