D. Phoenix and Science

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

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

inputCopy
3
9
11
2
outputCopy
3
1 0 2 
3
1 1 2
1
0 

Note

In the first test case, the following process results in bacteria with total mass 9:
Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5 each.
Night 1: All bacteria’s mass increases by one. There are now two bacteria with mass 1.5.
Day 2: None split.
Night 2: There are now two bacteria with mass 2.5.
Day 3: Both bacteria split. There are now four bacteria with mass 1.25.
Night 3: There are now four bacteria with mass 2.25.
The total mass is 2.25+2.25+2.25+2.25=9. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights.
In the second test case, the following process results in bacteria with total mass 11:
Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5.
Night 1: There are now two bacteria with mass 1.5.
Day 2: One bacterium splits. There are now three bacteria with masses 0.75, 0.75, and 1.5.
Night 2: There are now three bacteria with masses 1.75, 1.75, and 2.5.
Day 3: The bacteria with mass 1.75 and the bacteria with mass 2.5 split. There are now five bacteria with masses 0.875, 0.875, 1.25, 1.25, and 1.75.
Night 3: There are now five bacteria with masses 1.875, 1.875, 2.25, 2.25, and 2.75.
The total mass is 1.875+1.875+2.25+2.25+2.75=11. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights.
In the third test case, the bacterium does not split on day 1, and then grows to mass 2 during night 1.

说是贪心,看了半天题解也还是没明白是什么意思,但是当自己尝试着写了几项之后立马就有了思路了。
主要还是考察贪心+找规律。

天数 b n − b n − 1 b_n-b_{n-1} bnbn1的最大值 第n天最大质量
1 1 1
2 2 3
3 4 7
4 8 15
…… …… ……
n 2 n − 1 2^{n-1} 2n1 2 n − 1 2^n-1 2n1

贪心的思想是尽量让每天增加的多一点,天数就会更少。
规律:
把这个表格列出来就很有规律可言了,发现第i-1天到第i天权值的增量(也就是第i-1个晚上的增量)和第i天的最大质量一一对应。并且第i-1天到第i天权值的增量之差便是分裂的个数(这是从权值增量最大值发现的,不是最大值的时候也是如此,可以尝试举例看看)。

code:

#include 
#include 
#include 
#define local
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int N = 1e5+5;
const ll inf=0x3f3f3f3f3f3f3f3f;
//const int inf=0x3f3f3f3f;
const int mod = 1e9+7;
const double eps=1e-10;
const int tmp=31;
using namespace std;

ll a[N],b[N];
ll pow_(int x){//求2的方幂
  if(!x) return 1;
  ll s=1;
  while(x--) s*=2;
  return s;
}
void init(){ //每天的最大质量
  for(int i=1;i<=32;i++) a[i] = pow_(i)-1;
}
int main()
{
#ifdef local
   // freopen("in.txt","r",stdin);
#endif // local
     init();
     int _;
     cin>>_;
     while(_--){
        for(int i=1;i<=32;i++) b[i] = (a[i]+1)/2;//权值的最大增量
        int n;
        cin>>n;
        int p = lower_bound(a+1,a+32+1,n)-a;
        cout<<p-1<<endl;
        if(a[p]==n)
            for(int i=2;i<=p;i++) cout<<b[i]-b[i-1]<<" ";
        else {
            b[33] = n-a[p-1];
            sort(b+1,b+33+1);
            for(int i=2;i<=p;i++) cout<<b[i]-b[i-1]<<" ";
        }
        puts("");
     }
     return 0;
}


你可能感兴趣的:(思维,贪心)