F 三角形
链接:https://ac.nowcoder.com/acm/contest/5758/F
来源:牛客网
小明有一根长度为a的木棒,现在小明想将木棒分为多段(每段木棒长度必须为整数),
使得分隔后的木棍中,取出的任意三段都不能构成三角形,小明想知道木棒最多被分成几段?
题解
不能构成三角形则考虑斐波那契数列。
按斐波那契数列不断地从木棒中减去某个长度,直到木棒长度小于等于下一个斐波那契数。
注意题目数据比较大,要用ull。
AC代码
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define pre(i, x, y) for(int i = x; i >=y; i--)
#define INF (0x3f3f3f3f)
#define mod (1e9+7)
using namespace std;
int main() {
int T;
IO;
cin >> T;
while (T--) {
ull n;
cin >> n;
ull sum = 0;
ull a = 1, b = 1, c = a + b;
while (a <= n) {
sum++;
n-=a;
a = b;
b = c;
c = a + b;
}
cout << sum << endl;
}
return 0;
}