首页 技术 正文
技术 2022年11月14日
0 收藏 826 点赞 4,452 浏览 1342 个字

题目:Fibonacci Numbers

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3117

分析:

1)后四位可以用矩阵快速幂解决。$T= \left[ \begin{array}{cc} 0 & 1 \\ 1 & 1 \end{array} \right] $

2)前四位:Fibonacci公式:$ans= \frac{1}{\sqrt{5}} * { ( {\frac{1+sqrt{5}}{2}}^n – {\frac{1-\sqrt{5}}{2}}^n )}$

当n够大时,${( \frac{1-\sqrt{5}}{2} )}^n$趋于0。

取以10为底的对数:

$log_{10}{ans} = log_{10}{\frac{1}{\sqrt{5}} * {[ {\frac{1+sqrt{5}}{2}}^n – {\frac{1-\sqrt{5}}{2}}^n ]}}$

$= log_{10}{\frac{1}{\sqrt{5}} * {[ \frac{1+sqrt{5}}{2} ]}^n }$

$= log_{10}{\frac{1}{\sqrt{5}} + log_{10}{\frac{1+sqrt{5}}{2}}^n}$

$= -0.5*log_{10}{5} + n*log_{10}{[ \frac{1+sqrt{5}}{2} ]}$

然后

$10^{ans} \% 10000$=$10^{[ans]} * 1000$=$10^{3+ans-(long long)ans}$

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
const int MOD=;
struct Matrix{
LL a[][];
void init(int f){
memset(a,,sizeof a);
if(f==-)return;
for(int i=;i<;++i)a[i][i]=;
}
};
Matrix operator*(Matrix& A,Matrix& B){
Matrix C;C.init(-);
for(int i=;i<;++i)
for(int j=;j<;++j)
for(int k=;k<;++k){
C.a[i][j]+=A.a[i][k]*B.a[k][j];
C.a[i][j]%=MOD;
}
return C;
}
Matrix operator^(Matrix A,int n){
Matrix Rt;Rt.init();
for(;n;n>>=){
if(n&)Rt=Rt*A;
A=A*A;
}
return Rt;
}
int main(){
Matrix A,T;
T.a[][]=;T.a[][]=;
T.a[][]=;T.a[][]=;
for(int n;~scanf("%d",&n);){
if(n<){
A=T^n;
printf("%lld\n",A.a[][]);
}else{
A=T^n;
double ans=-0.5*log10(5.0)+n*log10((+sqrt(5.0))/);
ans=ans-(LL)ans+;
printf("%d...%04lld\n",(int)pow(,ans),A.a[][]%);
}
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289