hdu 3117 Fibonacci Numbers_the fibonacci sequence is the sequence of numbers _ITAK的博客-程序员秘密

技术标签: ACM_费波那契  ACM_矩阵+高斯  斐波那契  ACM_HDU  

点击此处即可传送到hdu 3117

                  **Fibonacci Numbers**


Problem Description

The Fibonacci sequence is the sequence of numbers such that every element is equal to the sum of the two previous elements, except for the first two elements f0 and f1 which are respectively zero and one.



What is the numerical value of the nth Fibonacci number? 



Input

For each test case, a line will contain an integer i between 0 and 108 inclusively, for which you must compute the ith Fibonacci number fi. Fibonacci numbers get large pretty quickly, so whenever the answer has more than 8 digits, output only the first and last 4 digits of the answer, separating the two parts with an ellipsis (“...”). 

There is no special way to denote the end of the of the input, simply stop when the standard input terminates (after the EOF).





Sample Input

0
1
2
3
4
5
35
36
37
38
39
40
64
65





Sample Output

0
1
1
2
3
5
9227465
14930352
24157817
39088169
63245986
1023...4155
1061...7723
1716...7565

题目大意:就是给你一个数如果<10^8就正常输出它的斐波那契数,否则就输出前四位和后四位

解题思路:矩阵连乘和封闭公式结合:
f(n)=1/sqrt(5)(((1+sqrt(5))/2)^n+((1-sqrt(5))/2)^n)

具体详见代码:

/*
2015 - 8 - 13 下午
Author: ITAK
今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
*/
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
LL Fei[100];
const int mod = 10000;
const int maxn = 2;
typedef struct
{
    LL m[maxn][maxn];
} Matrix;

Matrix P = {
   0,1,
            1,1
           };
Matrix I = {
   1,0,
            0,1
           };
Matrix Mat_mul(Matrix a, Matrix b)//矩阵乘法
{
    int i, j, k;
    Matrix c;//计位
    for(int i=0; i<maxn; i++)
        for(int j=0; j<maxn; j++)
        {
            c.m[i][j] = 0;
            for(int k=0; k<maxn; k++)
                c.m[i][j] += (a.m[i][k]*b.m[k][j])%mod;
            c.m[i][j] %= mod;
        }
    return c;
}
Matrix quick_mod(LL n)//快速幂
{
    Matrix m = P, ans = I;
    while(n)
    {
        if(n & 1)
            ans = Mat_mul(ans, m);
        n >>= 1;
        m = Mat_mul(m,m);
    }
    return ans;
}
int main()
{
    double log_s = 0.0;
    Matrix tmp;
    int n, bit=0;
    Fei[0] = 0;
    Fei[1] = 1;
    for(int i=2; i<40; i++)
        Fei[i] = Fei[i-1]  +Fei[i-2];
    while(~scanf("%d",&n))
    {
        if(n < 40)
            printf("%d\n",Fei[n]);
        else
        {
            tmp = quick_mod(n);
            int ans_e = tmp.m[0][1];
            log_s=log10(1.0/sqrt(5)) + (double)n*log10((1.0+sqrt(5))/2.0);
            int ans_s=(int)(pow(10,log_s-(int)log_s+3));
            printf("%d",ans_s) ;
            printf("...");
            printf("%04d\n",ans_e);
        }
    }
    return 0;
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qingshui23/article/details/47618353

智能推荐

使用媒体查询在屏幕宽度改变的时候实时更改样式_LPLIFE的博客-程序员秘密

 媒体查询需要一层层查找下去修改,否则无效当屏幕宽度小于1024的时候,.time-box里头的.data内容被隐藏 &amp;lt;li class=&quot;time-box&quot;&amp;gt; &amp;lt;div class=&quot;time&quot;&amp;gt;{{systemTime}}&amp;lt;/div&amp;gt; &amp;lt;div class=&quot;date&quot;&amp;

web前端学习83-86(CSS引入方式---内部样式表,行内样式表,外部样式表)_都取样式表时,内部,行内、链接三种方法插入的不同web_向long.js学习的博客-程序员秘密

文章目录5 CSS 引入方式5.1 CSS的三种样式表5.2 内部样式表5.3 行内样式表5.4 外部样式表5.5 CSS引入方式总结5 CSS 引入方式5.1 CSS的三种样式表前面例子在写CSS时都是写在style中,但是并不是都写在里面的按照CSS样式书写的位置(或者引入的方式),CSS样式表可以分为三大类:行内样式表(行内式)内部样式表(嵌入式)外部样式表(链接式)5.2 内部样式表内部样式表(内嵌样式表)是写到html页面内部。是将所有的CSS代码抽取出来,单独放到一个*

Windows、Linux、ARM、Android、iOS全平台支持的RTMP推流组件EasyRTMP- iOS进入预览界面系统直接崩溃的原因分析_EasyNVR的博客-程序员秘密

EasyRTMP是一套调用简单、功能完善、运行高效稳定的RTMP推流功能组件,支持RTMP推送断线重连、环形缓冲、智能丢帧、网络事件回调,支持Windows、Linux、ARM、Android、iOS平台,支持市面上绝大部分的RTMP流媒体服务器,能够完美应用于各种行业的直播需求,手机直播、桌面直播、摄像机直播、课堂直播等方面。EasyRTMP推流功能特点- 调用简单无论是个...

内网渗透测试:域内权限维持思路总结_域环境设置进程的运行权限_zhang三的博客-程序员秘密

我的Freebuf:https://www.freebuf.com/author/MrAnonymous我的博客:https://whoamianony.top/文章目录Windows 操作系统常见持久性后门Windows系统隐藏账户Shift 粘滞键后门(1)手动制作(2)Empire 下的利用注册表键后门(1)手动制作(2)Metasploit 下的利用(3)Empire 下的利用Windows 计划任务后门(1)利用 at 命令(2)利用 schtasks 命令(3)利用SharPersist工.

可视化 nlp_使用nlp可视化尤利西斯_柠檬大饭饭的博客-程序员秘密

可视化 nlpMy data science experience has, thus far, been focused on natural language processing (NLP), and the following post is neither the first nor last which will include the novel Ulysses, by James ...

java速查手册.chm下载_C语言函数速查手册_吃土皮皮虎的博客-程序员秘密

本手册为chm格式,查询C语言常用函数非常方便字符串函数bcmp,bcopy,bzero,memccpy,memchr,memcmp,memcpy,memicmp,memmove,memset,movmem,setmem,stpcpy,strcat,strchr,strcmp,strcmpi,strcpy,strcspn,strdup,stricmp,strlen,strlwr,strncat,s...

随便推点

Android Fragment 使用讲解_android fragment使用_高、远的博客-程序员秘密

【1】Fragment是什么?相关介绍:Google官网给的解释是:Fragment 表示 FragmentActivity 中的行为或界面的一部分。您可以在一个 Activity 中组合多个片段,从而构建多窗格界面,并在多个 Activity 中重复使用某个片段。您可以将片段视为 Activity 的模块化组成部分,它具有自己的生命周期,能接收自己的输入事件,并且您可以在 Activity 运行时添加或移除片段(这有点像可以在不同 Activity 中重复使用的“子 Activity”...

Python虚拟环境(pipenv、venv、conda一网打尽)[通俗易懂]_pipenv和conda_M_qsqsqsq的博客-程序员秘密

要搞清楚什么是虚拟环境,首先要清楚Python的环境指的是什么。python哪里来?这个主要归功于配置的系统环境变量PATH,当我们在命令行中运行程序时,系统会根据PATH配置的路径列表依次查寻是否有可执行文件python(在windows中,省略了后缀.exe),当查寻到该文件时,执行该文件;'python' 不是内部或外部命令,也不是可运行的程序或批处理文件。test.py代码中import的模块在哪里找?import的模块包含两类,一类称为标准库,随着python的安装而安装;

解决No thread-bound request found: Are you referring to request attributes outside of an actual web re_lvxiucai的博客-程序员秘密

使用背景:今天在spring-cloud项目中,使用多线程异步调用微服务出现的错误Nothread-boundrequestfound:Areyoureferringtorequestattributesoutsideofanactualwebrequest,orprocessingarequestoutsideoftheoriginallyr...

信用评分之二--信用评分中的评分卡中的A卡、B卡和C卡_小小她爹的博客-程序员秘密

传统信用评分中的评分卡中的A卡、B卡和C卡是什么意思。

f-string字符串的使用_缘 源 园的博客-程序员秘密

# f-strings的目的:对字符串里面的动态数据进行设置# f字符串绑定数据的时候,需要使用{相关的数据/变量}# name = input("请输入姓名:")# age = input("请输入年龄:")## # 提示: fstrings设置数据使用: {数据/变量}# msg = f"姓名: {name} 年龄: {age}"# print(msg, type(msg))# 需求: 3 + 4 = 7num1 = float(input("请输入第一个数字:"))num.

国内Homebrew安装太慢 - 简单五步快速安装_homebrew安装慢_Vincent_Hsi的博客-程序员秘密

Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。 本文主要解决问题:Homebrew常规安装太慢;以及通过brew install安装软件太慢,还有时不时的自动updating巨耗时的问题。

推荐文章

热门文章

相关标签