#include <iostream>
#include <vector>
using namespace std;
//求出所有不大于s的k-bonacci数,放到 f 中,然后每次贪最接近s的数,放入 ans 中
vector<int>f;
vector<int>ans;
int binary_search(int target){
int low = 1, high = f.size() - 1, mid;
while (low + 1 < high){
mid = (low + high) / 2;
if (f[mid] >= target)high = mid - 1;
else low = mid;
}
if (f[high] < target)return high;
return low;
}
int main(){
int s, k;
cin >> s >> k;
f.push_back(0);
f.push_back(1);
int sum = 1;
f.push_back(sum);
while (sum < s){
int tmp = 0;
if ((int)f.size() - 1 - k > 0)
tmp = f[f.size() - 1 - k];
sum = sum * 2 - tmp;
f.push_back(sum);
}
int num = s;
while (num>0){
int index = binary_search(num);
if (f[index + 1] == num){
ans.push_back(f[index + 1]);
num = 0;
}
else{
ans.push_back(f[index]);
num -= f[index];
}
}
ans.push_back(0);
cout << ans.size() << endl;
cout << ans[0];
for (int i = 1; i < ans.size(); i++)cout << ' ' << ans[i];
cout << endl;
return 0;
}
CodeForces 225B Well-known Numbers(二分)
最新推荐文章于 2020-08-07 20:48:16 发布