http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4262
C++版本一
题解:
假设n长度的铁棍,可以这样分 1 1 2 3 5 8............
很眼熟是不是,---->斐波那契数列
所以对斐波那契数列求前缀和,当某一项的值小于等于n最大化,那么这一项的下标i就是可以分的段数。
搜索过程再用二分法优化一下
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q;
int ans,cnt,flag,temp;
ll a[N];
ll b[N];
char str;
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
//scanf("%d",&n);
//scanf("%d",&t);
//while(t--){}
a[2]=1;
a[1]=1;
cnt=2;
while(a[cnt]<=1000000000){
a[++cnt]=a[cnt-1]+a[cnt-2];//cout<<cnt<<" "<<a[cnt]<<endl;
}
cnt=0;
while(b[cnt]<=1000000000){
b[++cnt]=b[cnt-1]+a[cnt];//cout<<cnt<<" "<<b[cnt]<<endl;
}
while(~scanf("%d",&n)){
cout<<upper_bound(b+1,b+cnt+1,n)-b-1<<endl;
}
//cout << "Hello world!" << endl;
return 0;
}