1348D - Phoenix and Sciences[思维][好题]
time limit per test | memory limit per test | input | output |
---|---|---|---|
2 seconds | 256 megabytes | standard input | standard output |
Description:
Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.
Initially, on day 1 1 1, there is one bacterium with mass 1 1 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 m 2 \frac{m}{2} 2m each. For example, a bacterium of mass 3 3 3 can split into two bacteria of mass 1.5 1.5 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 n 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 ) t (1≤t≤1000) t(1≤t≤1000) — the number of test cases.
The first line of each test case contains an integer n ( 2 ≤ n ≤ 1 0 9 ) n (2≤n≤10^9) 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 -1 −1. Otherwise, print two lines.
The first line should contain an integer d d d — the minimum number of nights needed.
The next line should contain d d d integers, with the i i i-th integer representing the number of bacteria that should split on the i i i-th day.
If there are multiple solutions, print any.
Example input
3
9
11
2
Example output
3
1 0 2
3
1 1 2
1
0
Hit
In the first test case, the following process results in bacteria with total mass 9 9 9:
- Day 1 1 1: The bacterium with mass 1 1 1 splits. There are now two bacteria with mass 0.5 0.5 0.5 each.
- Night 1 1 1: All bacteria’s mass increases by one. There are now two bacteria with mass 1.5 1.5 1.5.
- Day 2 2 2: None split.
- Night 2: There are now two bacteria with mass 2.5 2.5 2.5.
- Day 3 3 3: Both bacteria split. There are now four bacteria with mass 1.25 1.25 1.25.
- Night 3 3 3: There are now four bacteria with mass 2.25 2.25 2.25.
The total mass is 2.25 + 2.25 + 2.25 + 2.25 = 9 2.25+2.25+2.25+2.25=9 2.25+2.25+2.25+2.25=9. It can be proved that 3 3 3 is the minimum number of nights needed. There are also other ways to obtain total mass 9 9 9 in 3 3 3 nights.
In the second test case, the following process results in bacteria with total mass 11 11 11:
- Day 1 1 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5.
- Night 1 1 1: There are now two bacteria with mass 1.5.
- Day 2 2 2: One bacterium splits. There are now three bacteria with masses 0.75, 0.75, and 1.5.
- Night 2 2 2: There are now three bacteria with masses 1.75 , 1.75 1.75, 1.75 1.75,1.75, and 2.5 2.5 2.5.
- Day 3 3 3: The bacteria with mass 1.75 1.75 1.75 and the bacteria with mass 2.5 2.5 2.5 split. There are now five bacteria with masses 0.875 , 0.875 , 1.25 , 1.25 0.875, 0.875, 1.25, 1.25 0.875,0.875,1.25,1.25, and 1.75 1.75 1.75.
- Night 3 3 3: There are now five bacteria with masses 1.875 , 1.875 , 2.25 , 2.25 1.875, 1.875, 2.25, 2.25 1.875,1.875,2.25,2.25, and 2.75 2.75 2.75.
The total mass is 1.875 + 1.875 + 2.25 + 2.25 + 2.75 = 11 1.875+1.875+2.25+2.25+2.75=11 1.875+1.875+2.25+2.25+2.75=11. It can be proved that 3 3 3 is the minimum number of nights needed. There are also other ways to obtain total mass 11 11 11 in 3 3 3 nights.
In the third test case, the bacterium does not split on day 1 1 1, and then grows to mass 2 2 2 during night 1 1 1.
分析:
题意:
最初只有一个
m
a
s
s
=
1
mass = 1
mass=1 的细菌
每天早上细菌可以选择分裂成两个,或者不分离
到了晚上过后,每个细菌的
m
a
s
s
mass
mass 都会增加
1
1
1
问最少使得
m
a
s
s
mass
mass 正好等于
n
n
n 的天数
以及每天分裂的细菌数
做法:
颓了,直到结束我都以为是不用正好等于,大于等于就可以
然后一直很迷茫明明只要一直分裂就可以了
o
r
z
orz
orz
显然如果是大于等于,很容易可以知道最少的天数为
t
=
l
o
g
2
n
t = log_2 n
t=log2n
如果每天每个细菌都分裂,那么每次
m
a
s
s
mass
mass 都会增加
2
k
2k
2k(
k
k
k 为分列前的细菌数),
k
→
2
k
k \rightarrow 2k
k→2k
如果选取这样的途径是最快到达接近
n
n
n 的
但是这样不一定可以正好到达
n
n
n
又可以知道如果选择不分裂,
m
a
s
s
mass
mass 增加的就只是
k
k
k(
k
k
k 为细菌个数)
显然我们可以将
n
=
2
1
+
2
2
+
.
.
.
+
2
t
+
(
x
)
n = 2^1+2^2+...+2^t + (x)
n=21+22+...+2t+(x)
这个
x
x
x 就是最后相差的值
可以相当于
2
i
,
x
,
2
i
+
1
2^i, x, 2^{i+1}
2i,x,2i+1, 其中
2
i
≤
x
≤
2
i
+
1
2^i \leq x \leq 2^{i+1}
2i≤x≤2i+1
最后输出的是分裂的细菌数,这里可以做下说明
假设原本为
2
1
2^1
21 个细菌,现在要使得
m
a
s
s
mass
mass 增加
2
2
2^2
22,则意味着需要
n
1
+
2
n
2
=
2
2
,
n
1
+
n
2
=
2
1
n_1 + 2n_2 = 2^2, n1+n_2=2^1
n1+2n2=22,n1+n2=21,
n
1
n_1
n1 表示未分裂的,
n
2
n_2
n2 表示要分裂的
两式相减正好就是
n
2
=
2
2
−
2
1
n_2 = 2^2 - 2^1
n2=22−21
又
n
2
n_2
n2 正好是我们要的分裂后得到的细菌数,因此最后输出就是排序后相邻两数的差值
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
const int maxm = 3e6 + 5;
vector<int> v;
int main() {
int T;
scanf("%d", &T);
while(T--) {
v.clear();
int n, sum = 0;
scanf("%d", &n);
for(int i = 1; sum + i <= n; i *= 2) {
sum += i, v.push_back(i); // 相当于第几天需要的细胞数
}
if(n - sum) v.push_back(n - sum);
sort(v.begin(), v.end());
printf("%d\n", (int)v.size() - 1);
for(int i = 1; i < v.size(); ++i)
printf("%d%c", v[i] - v[i - 1], i == (int)v.size() - 1 ? '\n' : ' ');
}
return 0;
}