Codeforces Round #638 (Div. 2) D. Phoenix and Science

Codeforces Round #638 (Div. 2) D. Phoenix and Science

題目鏈接
Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.

Initially, on day 1, there is one bacterium with mass 1.

Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass m splits, it becomes two bacteria of mass m2 each. For example, a bacterium of mass 3 can split into two bacteria of mass 1.5.

Also, every night, the mass of every bacteria will increase by one.

Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly n. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!

Input

The input consists of multiple test cases. The first line contains an integer t (1≤t≤1000) — the number of test cases.

The first line of each test case contains an integer n (2≤n≤109) — the sum of bacteria masses that Phoenix is interested in.

Output

For each test case, if there is no way for the bacteria to exactly achieve total mass n, print -1. Otherwise, print two lines.

The first line should contain an integer d — the minimum number of nights needed.

The next line should contain d integers, with the i-th integer representing the number of bacteria that should split on the i-th day.

If there are multiple solutions, print any.

Example

input

3
9
11
2

output

3
1 0 2 
3
1 1 2
1
0 

構造題~
一開始看題目完全沒思路,看了題解發現真的是太妙了😱
我們不難發現某天如果有 xx 個病毒,那變化的範圍恰爲 [x,2x][x,2*x],那麼題目就可以轉化爲構造一個數組,使得數組的元素和恰爲 nn,而我們發現病毒的變化恰好滿足 2 的指數冪,所以只要用 2 的倍數構造數組即可,但注意 n 可能會有剩餘,只要將剩餘部分插入數組重新排序即可,答案即爲數組中相鄰元素的差值,AC代碼如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
main(){
    int n,t;
    cin>>t;
    while(t--){
        cin>>n;
        vector<int>ans;
        for(int i=1;i<=n;i*=2) ans.push_back(i),n-=i;
        if(n>0){
            ans.push_back(n);
            sort(ans.begin(),ans.end());
        }
        cout<<ans.size()-1<<endl;
        for(int i=1;i<ans.size();i++) cout<<ans[i]-ans[i-1]<<" ";
        puts("");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章