Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

I. Problem description

In general, a positive integer can be divided into the sum of several positive integers.

If a positive integer can be split into several different powers of two, it is called a good split.

For example, 10=8+2=23+2110=8+2=2^3+2^110=8+2=23+21 is an excellent split. However, 7=4+2+1=22+21+207=4+2+1=2^2+2^ 07=4+2+1=22+21+20 is not a good split because 1 is not a positive integer power of 2.

Now, given a positive integer n, you need to determine whether there is a good split among all the splits of that number. If yes, please give specific split scheme.

Title link: Good split.

Two, the title requirements

Sample 1

Input: 10 Output: 8 2Copy the code

The sample 2

Input: 7 Output: -1Copy the code

inspection

1. Bit operation 2. The recommended time is 15 to 30 minutesCopy the code

Third, problem analysis

18. If you don’t know about bit operations, you can read this article in detail.

Day 45: Bit operation.

So basically, this is a question of whether a number can be divided into different powers of 2, not counting 202 to the 020.

If the input is odd, the output is negative 1, which does not exist.

Int 32 bits can store the amount of data in this problem, for example:

10->2 base: 10 10 Corresponds to 10 base: 8 0 2 0Copy the code

So we just need to move the binary of the number from the head of base 2 (i.e. 31 bits) to the right and calculate, if it is 1, output the corresponding base 10 result, if it is 0, skip.

Four, coding implementation

#include<iostream>
#include<math.h>
using namespace std;
int main(a)
{	
	long long  i,j,n,k;// Initialize the data
	cin>>n;/ / input n
	if(n%2! =0)// The output of odd numbers is -1
	{
		cout<<- 1;
		return 0;
	}
	else
	{
		for(i=31; i>=0; i--)// Right shift from base 2 to 31
		{
			if(n&(1<<i))// If the current bit is 1
			{
				k=pow(2,i);
				cout<<k<<"";// Output the result}}}return 0;
}
Copy the code

V. Test results