HDU 3117 Fibonacci Numbers 数学
http://acm.hdu.edu.cn/showproblem.php?pid=3117
fib是有一个数学公式的。

这里的是标准的fib公式
那么fib = 1 / sqrt(5) * ((1 + sqrt(5) / 2) ^ n - ((1 - sqrt(5)) / 2)^n) = 1 / sqrt(5) * (A^n - B^n)
那么,求后4位可以直接矩阵快速幂。
不能用上面公式的快速幂取模,因为存在精度误差。
然后求前4位的话,就是一个套路公式了。
在上一篇博客。http://www.cnblogs.com/liuweimingcprogram/p/6583154.html
这里因为后面的B很小,所以可以忽略。如果不忽略的话,就没办法做了。
比如你取A得前4位,减去B的前4位,那肯定错。比如123456789的前4位是1234,而1234的前4位又是1234,那么相减就等于0了。不过这里B的小数,前4位也是0.
然后还要除以sqrt(5),这个时候就需要你保留前5位,除以sqrt(5)后,再保留4位。
不然的话,比如你取了前4位是1234,除以sqrt(5),只剩下3位了。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = ;
struct Matrix {
LL a[maxn][maxn];
int row;
int col;
};
struct Matrix matrix_mul (struct Matrix a, struct Matrix b, int MOD) { //求解矩阵a*b%MOD
struct Matrix c = {}; //这个要多次用到,栈分配问题,maxn不能开太大,
//LL的时候更加是,空间是maxn*maxn的,这样时间用得很多,4和5相差300ms
c.row = a.row; //行等于第一个矩阵的行
c.col = b.col; //列等于第二个矩阵的列
for (int i = ; i <= a.row; i++) { //枚举第一个矩阵的行
for (int j = ; j <= b.col; j++) { //枚举第二个矩阵的列,其实和上面数值一样
for (int k = ; k <= b.row; k++) { //b中的一列中,有“行”个元素 notice
c.a[i][j] += a.a[i][k] * b.a[k][j]; //这里不及时取模,又有可能错!HDU 4565
}
c.a[i][j] = (c.a[i][j] + MOD) % MOD; //如果怕出现了负数取模的话。可以这样做
}
}
return c;
}
struct Matrix quick_matrix_pow(struct Matrix ans, struct Matrix base, int n, int MOD) {
//求解a*b^n%MOD
while (n) {
if (n & ) {
ans = matrix_mul(ans, base, MOD);//传数组不能乱传,不满足交换律
}
n >>= ;
base = matrix_mul(base, base, MOD);
}
return ans;
}
LL fib[];
void init() {
fib[] = ;
fib[] = ;
for (int i = ; i <= ; ++i) {
fib[i] = fib[i - ] + fib[i - ];
}
}
int n;
int calc(double a, LL b, int k) { //a^b的前k + 1位
double res = b * log10(a * 1.0) - (LL)(b * log10(a * 1.0)); //获得小数部分
return (int)(pow(10.0, k + res) / sqrt(5.0));
} int getMost() {
double a = ( + sqrt(5.0)) / ;
int res = calc(a, n, );
while (res >= ) res /= ;
return res;
}
int getLow() {
struct Matrix t1 = {};
t1.row = , t1.col = ;
t1.a[][] = , t1.a[][] = ; struct Matrix t2 = {};
t2.row = t2.col = ;
t2.a[][] = t2.a[][] = ;
t2.a[][] = , t2.a[][] = ; struct Matrix res = quick_matrix_pow(t1, t2, n - , );
return res.a[][];
}
void work() {
if (n <= ) {
cout << fib[n] << endl;
return;
}
int most = getMost();
int low = getLow();
printf("%04d...%04d\n", most, low);
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
init();
while (cin >> n) work();
return ;
}
HDU 3117 Fibonacci Numbers 数学的更多相关文章
- HDU 3117 Fibonacci Numbers(围绕四个租赁斐波那契,通过计++乘坐高速动力矩阵)
HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵高速幂) ACM 题目地址:HDU 3117 Fibonacci Numbers 题意: 求第n个斐波那契数的 ...
- hdu 3117 Fibonacci Numbers 矩阵快速幂+公式
斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...
- HDU 3117 Fibonacci Numbers(矩阵)
Fibonacci Numbers [题目链接]Fibonacci Numbers [题目类型]矩阵 &题解: 后4位是矩阵快速幂求,前4位是用log加Fibonacci通项公式求,详见上一篇 ...
- HDU 3117 Fibonacci Numbers( 矩阵快速幂 + 数学推导 )
链接:传送门 题意:给一个 n ,输出 Fibonacci 数列第 n 项,如果第 n 项的位数 >= 8 位则按照 前4位 + ... + 后4位的格式输出 思路: n < 40时位数不 ...
- hdu 3117 Fibonacci Numbers
这道题其实也是水题来的,求Fibonacci数的前4位和后4位,在n==40这里分界开.后4位不难求,因为n达到了10^18的规模,所以只能用矩阵快速幂来求了,但在输出后4位的时候一定要注意前导0的处 ...
- UVA 11582 Colossal Fibonacci Numbers(数学)
Colossal Fibonacci Numbers 想先说下最近的状态吧,已经考完试了,这个暑假也应该是最后刷题的暑假了,打完今年acm就应该会退了,但是还什么都不会呢? +_+ 所以这个暑假,一定 ...
- codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)(两种方法)
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 ...
- [HDU3117]Fibonacci Numbers
题目:Fibonacci Numbers 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3117 分析: 1)后四位可以用矩阵快速幂解决.$T= \left ...
- hdu Interesting Fibonacci
Interesting Fibonacci Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- MySQL索引优化-from 高性能MYSQL
Btree: 1. 尽量使用覆盖索引, 即三星索引 2. 多列索引如果带范围的话, 后续列不会作为筛选条件 3. 多列索引应选择过滤性更好的充当前缀索引 4. 尽量按主键顺序插入, 减少页分裂, 采用 ...
- hdu 1711 Number Sequence(kmp找子串第一次出现的位置)
题意:裸kmp 思路:kmp模板 #include<iostream> #include<stdio.h> #include<string.h> using nam ...
- WebApi发送HTML表单数据:文件上传与多部分MIME
5.3 Sending HTML Form Data5.3 发送HTML表单数据(2) 本文引自:http://www.cnblogs.com/r01cn/archive/2012/12/20/282 ...
- 机器学习 : 高斯混合模型及EM算法
Mixtures of Gaussian 这一讲,我们讨论利用EM (Expectation-Maximization)做概率密度的估计.假设我们有一组训练样本x(1),x(2),...x(m),因为 ...
- 「BZOJ3083」遥远的国度(树剖换根
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 4859 Solved: 1372[Submit][Status][Discu ...
- 从exgcd到exCRT
从最基础的开始. 1.gcd 这个不用说了吧--\(gcd(a,b) = gcd(b,a\%b)\),这个很显然. 2.exgcd 这玩意可以用来求形如\(ax+by = gcd(a,b)\)的不定方 ...
- SpringMVC笔记- 不配置HandlerMapping
使用SpringMVC框架时发现有的配置了HandlerMapping,而有的没有,那么它们有什么区别呢?不配置能不能正常使用框架呢? 下面我们看一看不配置任何HandlerMapping时,框架会使 ...
- SpringMVC配置字符过滤器的两种方式
有时候使用SpringMVC框架提交表单时会出现中文乱码,以下是我亲自试验过的配置字符过滤器的两种: 1.在web.xml中配置 <filter> <filter-name>c ...
- CF-798A
A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- xml的的特殊字符转义&
& ampersand 连接符 & " quotation 双引号 “ &apos apostrophe 单引号 ...