您的位置:首页 > 其它

HDU 1568 Fibonacci(斐波那契前四位)

2014-08-05 01:15 387 查看


HDU 1568 Fibonacci(斐波那契前四位)

tags: ACM

题目地址:HDU 1568 Fibonacci

题意:

中文题。

分析:

用取对的方法,真是涨姿势啊。

代码:
/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  Blog:        http://blog.csdn.net/hcbbt *  File:        1568.cpp
*  Create Date: 2014-08-04 10:09:58
*  Descripton:  fib
*/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define repf(i,a,b) for(int i=(a);i<=(b);i++)

typedef long long ll;

int n;
ll tab[22];
double ans;

double fib(int x) {
    return -0.5 * log(5.0) / log(10.0) + ( (double)n) * log((sqrt(5.0) + 1) / 2) / log(10.0);
}

int main() {
    // table
    tab[0] = 0;
    tab[1] = 1;
    repf (i, 2, 20)
        tab[i] = tab[i - 1] + tab[i - 2];

    while (~scanf("%d", &n)) {
        if (n < 21) {
            printf("%lld\n", tab
);
            continue;
        }
        ans = fib(n);
        ans -= floor(ans);
        ans = pow(10.0, ans);
        while (ans < 1000)
            ans *= 10;
        printf("%d\n", (int)ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: