题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1568

题意:如标题所示,求斐波那契数前四位,不足四位直接输出答案

斐波那契数列通式:

当n<=20的时候,不足四位,所以直接打表。

当n>20的时候,大于四位的时候,ans满足这个公式:ans=-0.5*log10(5.0)+num*1.0*log10((1+sqrt(5.0))/2.0);

这个公式是怎么来的呢?我们可以对an取10的对数,根据对数的性质。

log10(ans)=log10(1/sqrt(5))+log10(((1+sqrt(5))/2)^num-((1-sqrt(5))/2)^num))

log10(ans)=0-0.5*log10(5.0)+log10(((1+sqrt(5))/2)^num-((1-sqrt(5))/2)^num)),当num趋于无穷的的时候  。

lim((1-sqrt(5))/2)^num)=0

log10(ans)=0-0.5*log10(5.0)+log10(((1+sqrt(5))/2)^num)= -0.5*log10(5.0)+num*1.0*log10( (1+sqrt(5))/2),我们就得到了上文的公式。

这里说一下原理,x=123456789,那么y=log10(x)=1.23456789,这个时候将y*=1000,就得到了 y=1234.56789,求幂次和取对数互为逆运算,通过这个原理我们可以求前x的长度。

//Author: xiaowuga
#include <bits/stdc++.h>
#define maxx INT_MAX
#define minn INT_MIN
#define inf 0x3f3f3f3f
#define maxn
using namespace std;
typedef long long ll;
ll table[];
int main() {
ios::sync_with_stdio(false);cin.tie();
table[]=;table[]=;
for(int i=;i<=;i++) table[i]=table[i-]+table[i-];
ll num;
while(cin>>num){
if(num<=) cout<<table[num]<<endl;
else{
double ans=-0.5*log10(5.0)+num*1.0*log10((+sqrt(5.0))/2.0);
ans=ans-(ll)ans;
double a=pow(10.0,ans);
a=*a;
cout<<(ll)a<<endl;
}
}
return ;
}

hdu3117:Fibonacci Numbers

这道题求斐波那契的数列的前四位和后四位,前四位和1568是一样的,后四位只需要把mod变成10000就行了,比较简单,直接看代码吧!!

//Author: xiaowuga
#include <bits/stdc++.h>
#define maxx INT_MAX
#define minn INT_MIN
#define inf 0x3f3f3f3f
#define n 2
#define MOD 10000
using namespace std;
typedef long long ll;
ll table[];
ll first_four(ll num){
double ans=-0.5*log10(5.0)+num*1.0*log10((+sqrt(5.0))/2.0);
ans=ans-(ll)ans;
double a=pow(10.0,ans);
a=*a;
return (ll)a;
}
struct Matrix{
ll mat[][];
Matrix operator * (const Matrix & m) const{
Matrix tmp;
for(int i=;i<n;i++)
for(int j=;j<n;j++){
tmp.mat[i][j]=;
for(int k=;k<n;k++){
tmp.mat[i][j]+=mat[i][k]*m.mat[k][j]%MOD;
tmp.mat[i][j]%=MOD;
}
}
return tmp;
}
};
Matrix POW(Matrix &m,ll k){
Matrix ans;
memset(ans.mat,,sizeof(ans.mat));
for(int i=;i<n;i++) ans.mat[i][i]=;
while(k){
if(k&) ans=ans*m;
k/=;
m=m*m;
}
return ans;
}
int main() {
ios::sync_with_stdio(false);cin.tie();
table[]=;table[]=;
for(int i=;i<=;i++) table[i]=table[i-]+table[i-];
ll num;
while(cin>>num){
if(num<=) cout<<table[num]<<endl;
else{
cout<<first_four(num)<<"...";
Matrix m;
memset(m.mat,,sizeof(m.mat));
m.mat[][]=m.mat[][]=m.mat[][]=;m.mat[][]=;
Matrix ans=POW(m,num-);
cout.fill('');
cout.width();
cout<<ans.mat[][]%MOD<<endl;
}
}
return ;
}

hdu1568&&hdu3117 求斐波那契数前四位和后四位的更多相关文章

  1. C++求斐波那契数

    题目内容:斐波那契数定义为:f(0)=0,f(1)=1,f(n)=f(n-1)+f(n-2)(n>1且n为整数) 如果写出菲氏数列,则应该是: 0 1 1 2 3 5 8 13 21 34 …… ...

  2. POJ 3070(求斐波那契数 矩阵快速幂)

    题意就是求第 n 个斐波那契数. 由于时间和内存限制,显然不能直接暴力解或者打表,想到用矩阵快速幂的做法. 代码如下: #include <cstdio> using namespace ...

  3. HDU 1568 Fibonacci【求斐波那契数的前4位/递推式】

    Fibonacci Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Proble ...

  4. 求斐波那契数的python语言实现---递归和迭代

    迭代实现如下: def fab(n): n1 = 1 n2 = 1 if n<1: print("输入有误!") return -1 while (n-2)>0: n3 ...

  5. 数学算法(一):快速求斐波那契数第n项通过黄金分割率公式

    有一个固定的数学公式= =,不知道的话显然没法应用 首先黄金分割率接近于这个公式, (以下为黄金分割率与斐波那契的关系,可跳过) 通过斐波那契数列公式 两边同时除以 得: (1) 注意后一项比前一项接 ...

  6. Python - 求斐波那契数列前N项之和

    n = int(input("Input N: ")) a = 0 b = 1 sum = 0 for i in range(n): sum += a a, b = b, a + ...

  7. 算法笔记_001:斐波那契数的多种解法(Java)

    本篇文章解决的问题来源于算法设计与分析课程的课堂作业,主要是运用多种方法来计算斐波那契数.具体问题及解法如下: 一.问题1: 问题描述:利用迭代算法寻找不超过编程环境能够支持的最大整数的斐波那契数是第 ...

  8. codeforce 227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质

    E. Anniversary time limit per test2 seconds memory limit per test256 megabytes inputstandard input o ...

  9. 用x种方式求第n项斐波那契数,99%的人只会第一种

    大家好啊,我们又见面了.听说有人想学数据结构与算法却不知道从何下手?那你就认真看完本篇文章,或许能从中找到方法与技巧.     本期我们就从斐波那契数列的几种解法入手,感受算法的强大与奥妙吧. 原文链 ...

随机推荐

  1. jQuery 插件分享-非常优秀的tab插件tabulous- 学徒帮

    干货jquery插件分享之tab. tab 选项卡切换,在日常开发中也是一种比较常见的呈现控件,今天这个tab控件效果还是蛮喜欢的,推荐给大家有用到的场景可以试试: tabulous.js A jQu ...

  2. xadmin 问题总结

    pip install django-import-export

  3. Unix系统编程()复制文件描述符

    Bourne shell的IO重定向语法2>&1,意在通知shell把标准错误(文件描述符2)重定向到标准输出(文件描述符1).因此下列命令将把标准输出和标准错误写入result.log ...

  4. PostgreSQL 配置远程访问

    配置远 程连接PostgreSQL数据库的步骤很简单,只需要修改data目录下的pg_hba.conf和postgresql.conf. pg_hba.conf:配置对数据库的访问权限, postgr ...

  5. 终极方法,pjsip发起多方对讲出错Too many objects of the specified type (PJ_ETOOMANY)

    http://blog.csdn.net/zhangjm_123/article/details/26727221 —————————————————————————————————————————— ...

  6. PHP多进程编程(3):多进程抓取网页的演示

    我们知道,从父进程到子经常的数据传递相对比较容易一些,但是从子进程传递到父进程就比较的困难. 有很多办法实现进程交互,在php中比较方便的是 管道通信.当然,还可以通过 socket_pair 进行通 ...

  7. 关于Aspose强大的应用--EXECL

    protected void btnConfirg_Click(object sender, EventArgs e) { genExcel(); } //设置内容文字色 表中有一个蓝色文字列和绿色文 ...

  8. jquery库实现iframe自适应内容高度和宽度

    javascript原生和jquery库实现iframe自适应内容高度和宽度---推荐使用jQuery的代码! ‍<iframe src="index.php" id=&qu ...

  9. 数据库 proc编程七

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...

  10. selenium:chromedriver与chrome版本的对应关系

    转自:http://blog.csdn.NET/huilan_same/article/details/51896672 再使用selenium打开chrome浏览器的时候,需要用chromedriv ...