HDU 1568
给定n,0 <= n <= 100000000,斐波那契数列的第n项的前四位(不足则全部输出)
利用不动点去解得斐波那契数列的公式,显然数据爆longlong,所以以10为底取对数即可
前20项不足10000,预处理会更方便
1 #include <cmath> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 #include <vector> 7 #define max(x, y) (x > y ? x : y) 8 #define min(x, y) (x > y ? y : x) 9 #define INF 0x3f3f3f3f 10 #define mod 1000000007 11 #define Yes printf("Yes\n") 12 #define No printf("No\n") 13 using namespace std; 14 typedef long long LL; 15 typedef pair<int, int> PII; 16 17 int n; 18 19 const int maxn = 30; 20 21 int f[maxn] = {0, 1, 1}; 22 23 int main(int argc, const char * argv[]) { 24 for (int i = 3; i <= 20; i++) { 25 f[i] = f[i - 1] + f[i - 2]; 26 } 27 //printf("%d\n", f[20]); 28 while (~scanf("%d", &n)) { 29 if (n <= 20) { 30 printf("%d\n", f[n]); 31 } else { 32 double tmp = -0.5 * log(5.0) / log(10.0) + ((double)n) * log((sqrt(5.0)+1.0)/2.0) / log(10.0); 33 tmp -= floor(tmp); 34 tmp = pow(10.0, tmp); 35 while (tmp < 1000) tmp *= 10; 36 printf("%d\n", (int)(tmp)); 37 } 38 } 39 return 0; 40 }