[HDU3117]Fibonacci Numbers
题目: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 ;
}
[HDU3117]Fibonacci Numbers的更多相关文章
- 【HDU3117】Fibonacci Numbers
[HDU3117]Fibonacci Numbers 题面 求斐波那契数列的第\(n\)项的前四位及后四位. 其中\(0\leq n<2^{32}\) 题解 前置知识:线性常系数齐次递推 其实后 ...
- codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)(两种方法)
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 ...
- Codeforces 446-C DZY Loves Fibonacci Numbers 同余 线段树 斐波那契数列
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- cf446C DZY Loves Fibonacci Numbers
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers
參考:http://www.cnblogs.com/chanme/p/3843859.html 然后我看到在别人的AC的方法里还有这么一种神方法,他预先设定了一个阈值K,当当前的更新操作数j<K ...
- HDU 3117 Fibonacci Numbers(围绕四个租赁斐波那契,通过计++乘坐高速动力矩阵)
HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵高速幂) ACM 题目地址:HDU 3117 Fibonacci Numbers 题意: 求第n个斐波那契数的 ...
- Fibonacci Numbers
Fibonacci Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- Codeforces446C - DZY Loves Fibonacci Numbers
Portal Description 给出一个\(n(n\leq3\times10^5)\)个数的序列,进行\(m(m\leq3\times10^5)\)次操作,操作有两种: 给区间\([L,R]\) ...
- UVA 11582 Colossal Fibonacci Numbers(数学)
Colossal Fibonacci Numbers 想先说下最近的状态吧,已经考完试了,这个暑假也应该是最后刷题的暑假了,打完今年acm就应该会退了,但是还什么都不会呢? +_+ 所以这个暑假,一定 ...
随机推荐
- mysql一主多从配置详情
https://www.cnblogs.com/zgx/archive/2011/09/13/2174823.html 1.准备好3台虚机,一台master,两台slave且都安装好mysql 2.主 ...
- c++函数overload 的歧义匹配
https://www.zhihu.com/question/20200615 函数重载选择最佳匹配函数涉及到类型转换,默认参数 注意:没有int f(int,int)版本,编译器认为上面两个函数都是 ...
- 3.创建线程的第二种写法,继承threading.Tread类,重写run方法
#创建线程的第二种写法 #1.自定义一个类 #2.继承Thread #3.重写run()方法 import threading,time,random class MyThread(threading ...
- mybatis 批量update报语法错误解决方法
1.为什么会报语法错误 原因:在 *.xml文件内使用了循环,在mybatis中默认是不允许使用批量修改. <update id="setMaxMin" parameterT ...
- [BZOJ2438]杀人游戏
Description 一位冷血的杀手潜入 Na-wiat,并假装成平民.警察希望能在 N 个人里面,查出谁是杀手.警察能够对每一个人 进行查证,假如查证的对象是平民,他会告诉警察,他认识的人, 谁是 ...
- adb常用命令-android学习第一天
转载出处:http://www.cnblogs.com/xiaoxuetu/ 转载来源:https://www.cnblogs.com/xiaoxuetu/p/3411214.html 平时开发and ...
- AI-IBM-cognitive class --Liner Regression
Liner Regression import matplotlib.pyplot as plt import pandas as pd import pylab as pl import numpy ...
- vue,一路走来(11)--HTML5 History模式
HTML5 History模式 项目中我用的是history模式. 默认 hash 模式 —— 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载. 如果不 ...
- vue,一路走来(6)--微信支付
微信支付 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6 分享一下vue实现微信支付.在微信浏览器里面 ...
- nginx下TP3.2访问页面总是404
这是我测试onethink用的配置 可以参考一下 server { listen 80; server_name onethink.dev www.onethink.dev; root /vagran ...