【HDU3117】Fibonacci Numbers

题面

求斐波那契数列的第\(n\)项的前四位及后四位。

其中\(0\leq n<2^{32}\)

题解

前置知识:线性常系数齐次递推

其实后四位还是比较好求,矩阵快速幂就可以了,主要是前四位。

先用线性常系数齐次递推求出斐波那契数列的通项公式

\[f_n=\frac{\sqrt 5}{5}\left((\frac{1+\sqrt5}{2})^n-(\frac{1-\sqrt5}{2})^n\right)
\]

因为数列的前\(39\)项我们还是存的下的,所以我们只考虑\(n\geq40\)的情况

考虑到\(n\geq40\)时\(\frac{\sqrt 5}{5}*(\frac{1-\sqrt5}{2})^n\)是个很小的东西,可以不考虑它的影响

那么我们就是要求

\[\frac{\sqrt 5}{5}(\frac{1+\sqrt5}{2})^n
\]

现在先考虑这样一个式子,数\(x\)用科学计数法表示

\[x=t*10^k
\]

那么\(x\)的前四位即为\(t\)的前四位,我们将\(x\)取个常用对数

\[\lg x=\lg t+k
\]

类比上式以及我们要求的式子:

\[\begin{aligned}
y&=\lg\left(\frac{\sqrt 5}{5}(\frac{1+\sqrt5}{2})^n\right)\\
&=\lg\frac{\sqrt 5}{5}+\lg\;(\frac{1+\sqrt5}{2})^n\\
&=\lg\frac{\sqrt 5}{5}+n\times \lg\frac{1+\sqrt5}{2}
\end{aligned}
\]

那么\(\lg t=y-\lfloor y\rfloor\),最后\(1000\times 10^y\)的整数部分就是答案。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int Mod = 1e4;
struct Matrix {
int m[2][2];
Matrix() { memset(m, 0, sizeof(m)); }
void init() { for (int i = 0; i < 2; i++) m[i][i] = 1; }
int *operator [] (int id) { return m[id]; }
Matrix operator * (const Matrix &b) {
Matrix res;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
res[i][j] = (res[i][j] + m[i][k] * b.m[k][j] % Mod) % Mod;
return res;
}
} ;
int N, f[40];
int TASK1() {
double s = log10(sqrt(5.0) / 5.0) + 1.0 * N * log10((1.0 + sqrt(5.0)) / 2.0);
s = s - (int)s;
double ans = 1000 * pow(10.0, s);
return ans;
}
int TASK2() {
Matrix S, T, ans; int n = N; ans.init();
S[0][0] = 1;
T[0][0] = 1, T[0][1] = 1;
T[1][0] = 1, T[1][1] = 0;
while (n) { if (n & 1) ans = ans * T; n >>= 1; T = T * T; }
S = ans * S;
return ans[1][0];
}
int main () {
f[0] = 0, f[1] = 1; for (int i = 2; i < 40; i++) f[i] = f[i - 1] + f[i - 2];
while (~scanf("%d", &N)) {
if (N < 40) { printf("%d\n", f[N]); continue; }
printf("%04d...%04d\n", TASK1(), TASK2());
}
return 0;
}

【HDU3117】Fibonacci Numbers的更多相关文章

  1. 【HDU1848】Fibonacci again and again(博弈论)

    [HDU1848]Fibonacci again and again(博弈论) 题面 Hdu 你有三堆石子,每堆石子的个数是\(n,m,p\),你每次可以从一堆石子中取走斐波那契数列中一个元素等数量的 ...

  2. 【CF55D】Beautiful numbers(动态规划)

    [CF55D]Beautiful numbers(动态规划) 题面 洛谷 CF 题解 数位\(dp\) 如果当前数能够被它所有数位整除,意味着它能够被所有数位的\(lcm\)整除. 所以\(dp\)的 ...

  3. 【CF628D】Magic Numbers 数位DP

    [CF628D]Magic Numbers 题意:求[a,b]中,偶数位的数字都是d,其余为数字都不是d,且能被m整除的数的个数(这里的偶数位是的是从高位往低位数的偶数位).$a,b<10^{2 ...

  4. 【CF55D】Beautiful numbers

    [CF55D]Beautiful numbers 题面 洛谷 题解 考虑到如果一个数整除所有数那么可以整除他们的\(lcm\),而如果数\(x\)满足\(x\bmod Lcm(1,2...,9)=r\ ...

  5. 【poj3070】 Fibonacci

    http://poj.org/problem?id=3070 (题目链接) 题意 用矩阵乘法求fibonacci数列的第n项. Solution 矩乘入门题啊,题目把题解已经说的很清楚里= =. 矩乘 ...

  6. 【类似N^N做法的斐波那契数列】【HDU1568】 Fibonacci

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

  7. 【HackerRank】Missing Numbers

    Numeros, The Artist, had two lists A and B, such that, B was a permutation of A. Numeros was very pr ...

  8. 【HackerRank】Closest Numbers

    Sorting is often useful as the first step in many different tasks. The most common task is to make f ...

  9. 【算法】Fibonacci(斐波那契数列)相关问题

    一.列出Fibonacci数列的前N个数 using System; using System.Collections.Generic; using System.Linq; using System ...

随机推荐

  1. Kali-linux使用Nessus

    Nessus号称是世界上最流行的漏洞扫描程序,全世界有超过75000个组织在使用它.该工具提供完整的电脑漏洞扫描服务,并随时更新其漏洞数据库.Nessus不同于传统的漏洞扫描软件,Nessus可同时在 ...

  2. [Python 多线程] threading.local类 (六)

    在使用threading.local()之前,先了解一下局部变量和全局变量. 局部变量: import threading import time def worker(): x = 0 for i ...

  3. rand7生成rand10,rand1生成rand6,rand2生成rand5(包含了rand2生成rand3)

    这种题要分两步,第一步是“插空儿”,第二步是“筛” 1.rand7生成rand10 只要是10的倍数就好 int rand10() { int num; do{ num = (rand7() - ) ...

  4. ERDAS IMAGINE 2014 32位 破解安装

    1.        安装Install ERDAS Foundation 2014 2.        安装ERDAS IMAGINE 2014  32位 3.        安装Intergraph ...

  5. cesium.js 设置缩放最大最小限制

    viewer.scene.screenSpaceCameraController.minimumZoomDistance = 1200;viewer.scene.screenSpaceCameraCo ...

  6. JavaScript前端将时间戳转换为日期格式

    function (data) { var date = new Date(data) var Y = date.getFullYear() + '-' var M = (date.getMonth( ...

  7. .gitignore设置不生效

    .gitignore git中,如果想要让git忽略某些文件,或不想push到远程库,不让其受版本的控制.可以使用git提供的.gitignore文件进行配置.像这样: 一般情况下,在文件还未修改前, ...

  8. objc单例的两种安全实现方案

    所有转出博客园,请您注明出处:http://www.cnblogs.com/xiaobajiu/p/4122034.html objc的单例的两种安全实现方案 首先应该知道单例的实现有两大类,一个是懒 ...

  9. 执行 sql 报错未缓冲查询,错误码 2014

    sql语句为 SELECT COUNT(id) AS tp_count FROM `tableName` WHERE `status` = 0 AND `source` = 1 AND ( `end_ ...

  10. 基于JQ的自定义弹窗组件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...