codeforces-618A-Slime Combining【位运算】

618A-Slime Combining

Your friend recently gave you some slimes for your birthday. You have n slimes all initially with value 1.

You are going to play a game with these slimes. Initially, you put a single slime by itself in a row. Then, you will add the other n - 1 slimes one by one. When you add a slime, you place it at the right of all already placed slimes. Then, while the last two slimes in the row have the same value v, you combine them together to create a slime with value v + 1.

You would like to see what the final state of the row is after you’ve added all n slimes. Please print the values of the slimes in the row from left to right.

Input
The first line of the input will contain a single integer, n (1 ≤ n ≤ 100 000).

Output
Output a single line with k integers, where k is the number of slimes in the row after you’ve finished the procedure described in the problem statement. The i-th of these numbers should be the value of the i-th slime from the left.

input
1
output
1
input
2
output
2
input
3
output
2 1
input
8
output
4

题目链接:cf-618A

题目大意:每次在后面加1,如果碰到两个相同的a == b,则变成一个数a+1

题目思路:本来想直接模拟,后来发现这个其实就是二进制的运算,好巧妙啊。

以下是代码:

//
// 618A.cpp
// codeforces
//
// Created by pro on 16/4/9.
// Copyright (c) 2016年 loy. All rights reserved.
//

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin >> n;
    for (int i = 20; i >= 0; i--)
    {
        if (n & (1 << i)) cout << i + 1 << " ";
    }
    return 0;
}

你可能感兴趣的:(位运算)