【HDU3117】Fibonacci Numbers
【HDU3117】Fibonacci Numbers
题面
求斐波那契数列的第\(n\)项的前四位及后四位。
其中\(0\leq n<2^{32}\)
题解
前置知识:线性常系数齐次递推
其实后四位还是比较好求,矩阵快速幂就可以了,主要是前四位。
先用线性常系数齐次递推求出斐波那契数列的通项公式
\]
因为数列的前\(39\)项我们还是存的下的,所以我们只考虑\(n\geq40\)的情况
考虑到\(n\geq40\)时\(\frac{\sqrt 5}{5}*(\frac{1-\sqrt5}{2})^n\)是个很小的东西,可以不考虑它的影响
那么我们就是要求
\]
现在先考虑这样一个式子,数\(x\)用科学计数法表示
\]
那么\(x\)的前四位即为\(t\)的前四位,我们将\(x\)取个常用对数
\]
类比上式以及我们要求的式子:
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的更多相关文章
- 【HDU1848】Fibonacci again and again(博弈论)
[HDU1848]Fibonacci again and again(博弈论) 题面 Hdu 你有三堆石子,每堆石子的个数是\(n,m,p\),你每次可以从一堆石子中取走斐波那契数列中一个元素等数量的 ...
- 【CF55D】Beautiful numbers(动态规划)
[CF55D]Beautiful numbers(动态规划) 题面 洛谷 CF 题解 数位\(dp\) 如果当前数能够被它所有数位整除,意味着它能够被所有数位的\(lcm\)整除. 所以\(dp\)的 ...
- 【CF628D】Magic Numbers 数位DP
[CF628D]Magic Numbers 题意:求[a,b]中,偶数位的数字都是d,其余为数字都不是d,且能被m整除的数的个数(这里的偶数位是的是从高位往低位数的偶数位).$a,b<10^{2 ...
- 【CF55D】Beautiful numbers
[CF55D]Beautiful numbers 题面 洛谷 题解 考虑到如果一个数整除所有数那么可以整除他们的\(lcm\),而如果数\(x\)满足\(x\bmod Lcm(1,2...,9)=r\ ...
- 【poj3070】 Fibonacci
http://poj.org/problem?id=3070 (题目链接) 题意 用矩阵乘法求fibonacci数列的第n项. Solution 矩乘入门题啊,题目把题解已经说的很清楚里= =. 矩乘 ...
- 【类似N^N做法的斐波那契数列】【HDU1568】 Fibonacci
Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HackerRank】Missing Numbers
Numeros, The Artist, had two lists A and B, such that, B was a permutation of A. Numeros was very pr ...
- 【HackerRank】Closest Numbers
Sorting is often useful as the first step in many different tasks. The most common task is to make f ...
- 【算法】Fibonacci(斐波那契数列)相关问题
一.列出Fibonacci数列的前N个数 using System; using System.Collections.Generic; using System.Linq; using System ...
随机推荐
- 【[NOI2009]管道取珠】
--\(shallwe\):这道题是\(noipDay2T2\)难度 好一个\(Day2T2\)难度啊,我觉得我可以退役了 平方和好像没有什么办法可以快速统计,于是考虑转化一下 我们可以将题意转化成这 ...
- ThinkPHP5入门(二)----控制器篇
一.控制器访问 1.命名空间 命名空间与目录路径对应. 如:路径位置为:application/index/controller/Index.php 其文件的命名空间应为:app\index\cont ...
- Java中的集合框架-Map
前两篇<Java中的集合框架-Commection(一)>和<Java中的集合框架-Commection(二)>把集合框架中的Collection开发常用知识点作了一下记录,从 ...
- 【原创】如何使用Jmockit进行单元测试
如何使用jmockit进行单元测试 1. Jmockit简介 JMockit 是用以帮助开发人员编写测试程序的一组工具和API,它完全基于 Java 5 SE 的 java.lang.instrume ...
- Oracle 11g DataGuard搭建(一) - 单节点到单节点
(一)DataGuard概要 DataGuard中文称为”数据卫士“,提供了数据库高可用性.数据保护和灾难恢复的功能.DataGuard通过建立primary数据库和standby数据库来确立参照关系 ...
- ORA-28002 密码过期解决方案
ORA-28002 密码过期解决方案 错误场景:当使用sqlplus进行登录时报错:ORA-28002 密码过期.错误原因:由于oracle 11g 在默认在default概要文件中设置了密码过期天数 ...
- @property & @synthesize & @dynamic 及相关属性作用探究
@property : iOS6 引入关键词. @property name; 指示编译器自动生成 name 的 setter 和 getter 方法 : - (NSString *)name; - ...
- POJ3074 Sudoku(lowbit优化搜索)
In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For exa ...
- 1001. 温度转换 (Standard IO)
1001. 温度转换 (Standard IO) 时间限制: 1000 ms 空间限制: 262144 KB 具体限制 题目描述 将输入的华氏温度转换为摄氏温度.由华氏温度F与摄氏温度C的转换 ...
- ABAP术语-World Wide Web
World Wide Web 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/21/1115728.html Internet service ...