#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
#define int long long
typedef pair<int,int> pii;
int lowbit(int x)
{
return x&(-x);
}
void solve()
{
int x;
cin>>x;
vector<int>g;
g.push_back(x);
while(x!=lowbit(x))
{
x-=lowbit(x);
g.push_back(x);
}
while(x!=1)
{
x/=2;
g.push_back(x);
}
cout<<g.size()<<endl;
for(auto x:g)cout<<x<<" ";
cout<<endl;
}
signed main()
{
int good_luck_to_you;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>good_luck_to_you;
while(good_luck_to_you--)
{
solve();
}
system("pause");
}
思路分析:
本题思路非常巧妙,我们把x转化为二进制,然后从最低位开始找1,这个我们就联想到lowbit的值就是最低位1加上后面的0的十进制的值,而且lowbit(x)是x的因子,所以我们每次减去lowbit(x)剩下的值就是所求,最后到x==lowbit(x),说明我们把该数变成了2的幂次,然后一直除以2直至为1即可。