Candies

C. Candies

Problem

This problem is about candy. Initially, you only have 1 1 1 candy, and you want to have exactly n n n candies.

You can use the two following spells in any order at most 40 40 40 times in total.

  • Assume you have x x x candies now. If you use the first spell, then x x x candies become 2 x − 1 2x-1 2x1 candies.
  • Assume you have x x x candies now. If you use the second spell, then x x x candies become 2 x + 1 2x+1 2x+1 candies.

Construct a sequence of spells, such that after using them in order, you will have exactly n n n candies, or determine it’s impossible.

Input

Each test contains multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases. Their description follows.

Each test case contains one line with a single integer n n n ( 2 ≤ n ≤ 1 0 9 2 \le n \le 10^9 2n109) — the required final number of candies.

Output

For each test case, output the following.

If it’s possible to eventually have n n n candies within 40 40 40 spells, in the first line print an integer m m m ( 1 ≤ m ≤ 40 1 \le m \le 40 1m40), representing the total number of spells you use.

In the second print m m m integers a 1 , a 2 , … , a m a_{1}, a_{2}, \ldots, a_{m} a1,a2,,am ( a i a_{i} ai is 1 1 1 or 2 2 2) separated by spaces, where a i = 1 a_{i} = 1 ai=1 means that you use the first spell in the i i i-th step, while a i = 2 a_{i} = 2 ai=2 means that you use the second spell in the i i i-th step.

Note that you do not have to minimize m m m, and if there are multiple solutions, you may output any one of them.

If it’s impossible, output − 1 -1 1 in one line.

Example

Input
4
2
3
7
17
Output
-1
1
2 
2
2 2 
4
2 1 1 1 

Note

For n = 3 n=3 n=3, you can just use the second spell once, and then have 2 ⋅ 1 + 1 = 3 2 \cdot 1 + 1 = 3 21+1=3 candies.

For n = 7 n=7 n=7, you can use the second spell twice. After the first step, you will have 3 3 3 candies. And after the second step, you will have 2 ⋅ 3 + 1 = 7 2 \cdot 3 + 1 = 7 23+1=7 candies.

Code

// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <stack>//栈
// #include <deque>//堆/优先队列
// #include <queue>//队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

void solve()
{
    ll n;
    cin>>n;

    vector<ll> a;
    if(n%2==0)
    {
        cout<<-1<<endl;
        return;
    }

    while(n>1)
    {
        if(((n-1)/2)&1)
        {
            a.push_back(2);
            n=(n-1)/2;
        }
        else if(((n+1)/2)&1)
        {
            a.push_back(1);
            n=(n+1)/2;
        }
    }

    cout<<a.size()<<endl;
    reverse(a.begin(),a.end());
    for(auto &t:a)
        cout<<t<<" ";
    cout<<endl;
}

int main()
{
    ll t;
    cin>>t;
    while(t--) solve();
    
    return 0;
}

这段程序的目的是计算将糖果均分给两个人所需的最小操作次数。让我们来分析一下为什么输入47会得到7作为结果。 当输入为47时,程序通过递归调用 `divide(candies, count, minCount)` 进行计算。初始调用是 `divide(47, 0, minCount)`。 首先,程序检查是否只剩下一个糖果。由于47不等于1,所以不满足条件。 接下来,程序检查47是否为偶数。由于47不是偶数,所以执行 `else` 分支。 在 `else` 分支中,程序进行了两个递归调用: 1. `divide(candies + 1, count + 1, minCount)`:这是将糖果数量加1的操作,并将操作次数加1。 2. `divide(candies - 1, count + 1, minCount)`:这是将糖果数量减1的操作,并将操作次数加1。 这两个递归调用会产生分支,并继续递归地进行计算。 对于第一个递归调用 `divide(candies + 1, count + 1, minCount)`,它会将糖果数量从47增加到48,并将操作次数从0增加到1。 接着,程序继续递归调用 `divide(candies // 2, count + 1, minCount)`,此时糖果数量为48。由于48是偶数,程序执行 `divide(candies // 2, count + 1, minCount)`,将糖果数量除以2,并将操作次数加1。 然后,程序继续递归调用 `divide(candies // 2, count + 1, minCount)`,此时糖果数量为24。同样地,程序将糖果数量除以2,并将操作次数加1。 接下来,程序继续递归调用 `divide(candies // 2, count + 1, minCount)`,此时糖果数量为12。同样地,程序将糖果数量除以2,并将操作次数加1。 继续递归调用 `divide(candies // 2, count + 1, minCount)`,此时糖果数量为6。同样地,程序将糖果数量除以2,并将操作次数加1。 接下来,程序继续递归调用 `divide(candies // 2, count + 1, minCount)`,此时糖果数量为3。由于3是奇数,程序将糖果数量加1,并将操作次数加1。 然后,程序继续递归调用 `divide(candies // 2, count + 1, minCount)`,此时糖果数量为4。同样地,程序将糖果数量除以2,并将操作次数加1。 最后,程序继续递归调用 `divide(candies // 2, count + 1, minCount)`,此时糖果数量为2。同样地,程序将糖果数量除以2,并将操作次数加1。 此时,糖果数量变为1,满足终止条件。程序将当前的操作次数1与 `minCount[0]` 中的值进行比较,并将较小值更新到 `minCount[0]` 中。 综上所述,最小操作次数为7。因此,输入47得到的结果是7。 如果你有任何其他问题,请告诉我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pretty Boy Fox

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值