题目链接

题意:斐波那契数列,当长度大于8时。要输出前四位和后四位

思路:后四位非常easy,矩阵高速幂取模,难度在于前四位的求解。 

已知斐波那契数列的通项公式:f(n) = (1 / sqrt(5)) * (((1 + sqrt(5)) / 2) ^ n - ((1 + sqrt(5)) / 2) ^ n)。当n >= 40时((1 + sqrt(5)) / 2) ^ n近似为0。

所以我们如果f(n) = t * 10 ^ k(t为小数),所以当两边同一时候取对数时。log10(t * 10 ^ k) = log10(t) + k = log10((1 / sqrt(5)) * (((1 + sqrt(5)) / 2))) = log10(1
/ sqrt(5)) + n * log10(((1 + sqrt(5)) / 2)))。然后减掉整数k。就能够得到log10(t),进而得到t值。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; //typedef long long ll;
typedef __int64 ll; const int MOD = 10000; struct mat{
ll s[2][2];
mat(ll a = 0, ll b = 0, ll c = 0, ll d = 0) {
s[0][0] = a;
s[0][1] = b;
s[1][0] = c;
s[1][1] = d;
}
mat operator * (const mat& c) {
mat ans;
memset(ans.s, 0, sizeof(ans.s));
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++) {
ans.s[i][j] = (ans.s[i][j] + s[i][k] * c.s[k][j]);
if (ans.s[i][j] >= 100000000)
ans.s[i][j] %= MOD;
}
return ans;
}
}c(1, 1, 1, 0), tmp(1, 0, 0, 1); ll n; mat pow_mod(ll k) {
if (k == 0)
return tmp;
else if (k == 1)
return c;
mat a = pow_mod(k / 2);
mat ans = a * a;
if (k % 2)
ans = ans * c;
return ans;
} int main() {
while (scanf("%I64d", &n) != EOF) {
if (n == 0)
printf("0\n");
else {
mat ans = pow_mod(n - 1);
if (n >= 40) {
double k = log10(1.0 / sqrt(5.0)) + (double)n * log10((1.0 + sqrt(5.0)) / 2.0);
double temp = k;
temp = k - (int)temp;
printf("%d...%.4I64d\n", (int)(1000.0 * pow(10.0, temp)), ans.s[0][0] % MOD);
}
else
printf("%I64d\n", ans.s[0][0]);
}
}
return 0;
}

HDU3117-Fibonacci Numbers(矩阵高速幂+log)的更多相关文章

  1. HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)

    HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意:  g(i)=k*i+b;i为变量.  给出 ...

  2. hdu 3117 Fibonacci Numbers 矩阵快速幂+公式

    斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...

  3. hdu 3306 Another kind of Fibonacci(矩阵高速幂)

    Another kind of Fibonacci                                                        Time Limit: 3000/10 ...

  4. HDU - 1588 Gauss Fibonacci (矩阵高速幂+二分求等比数列和)

    Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very cle ...

  5. Project Euler 435 Polynomials of Fibonacci numbers (矩阵快速幂)

    题目链接: https://projecteuler.net/problem=435 题意: The Fibonacci numbers $ {f_n, n ≥ 0}$ are defined rec ...

  6. HIT 2255 Not Fibonacci(矩阵高速幂)

    #include <iostream> #include <cstdio> #include <cstring> using namespace std; cons ...

  7. HDU1588-Gauss Fibonacci(矩阵高速幂+等比数列二分求和)

    题目链接 题意:g(x) = k * x + b.f(x) 为Fibonacci数列.求f(g(x)),从x = 1到n的数字之和sum.并对m取模. 思路:  设A = |(1, 1),(1, 0) ...

  8. UVA10518 - How Many Calls?(矩阵高速幂)

    UVA10518 - How Many Calls?(矩阵高速幂) 题目链接 题目大意:给你fibonacci数列怎么求的.然后问你求f(n) = f(n - 1) + f(n - 2)须要多少次调用 ...

  9. [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)

    Cellular Automaton Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 3048   Accepted: 12 ...

随机推荐

  1. 洛谷P1077 摆花(背包dp)

    P1077 摆花 题目描述 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共m盆.通过调查顾客的喜好,小明列出了顾客最喜欢的n种花,从1到n标号.为了在门口展出更多种花,规定第i种花不能 ...

  2. Gym - 101981G The 2018 ICPC Asia Nanjing Regional Contest G.Pyramid 找规律

    题面 题意:数一个n阶三角形中,有多少个全等三角形,n<=1e9 题解:拿到题想找规律,手画开始一直数漏....,最后还是打了个表 (打表就是随便定个点为(0,0),然后(2,0),(4,0), ...

  3. ScreenRecord(about C# winform)

    由于一些不得不做的事(哈,有时间再聊),所以就不得不写一个关于录屏的软件了,如此无力的开篇,开始吧. 在网上搜了很多关于录屏的源码,发现都使不了,剧情的需要很难满足.于是突然想到了github上的一个 ...

  4. Spring Cloud (3) 服务消费者-Ribbon

    在上一篇中使用LoadBalancerClient接口实现了获取某个服务的具体实例,并根据实例信息发起服务接口消费请求.但是这样的做法需要我们手工的区编写服务选取.连接拼接等繁琐的工作,对于开发人员来 ...

  5. python--7、面向对象

    什么是面向对象 对象,即抽象的一类事物中的某个具体的个体.这个世界中存在的一切皆为对象,不存在的也能创建出来. 较之面向过程的区别: 编程的复杂度远高于面向过程,不了解面向对象而立即上手基于它设计程序 ...

  6. Nginx服务的地址重写

    调整Nginx服务器配置,实现: 1.所有访问a.html的请求,重定向到b.html; 2.所有访问Nginx服务器(192.168.4.1)的请求重定向至www.baidu.com: 3.所有访问 ...

  7. vue系列---identify(生成图片验证码)插件

    identify 这是一个vue的插件,使用canvas来生成图形验证码. 具体参数如下: identify.vue组件(主要用于定义参数和方法) <template> <div c ...

  8. 单链表每k个节点一组进行反转(最后不足k个也反转)

    一道面试题,第一次碰到这道题的时候 要求10分钟之内手写代码实现,当时没写出来,后来花点时间把过程梳理一遍,也挺简单的....... 思路就是在原来单链表反转的基础上,加几个控制参数,记录几个关键节点 ...

  9. loadrunner安装方法

    1.loadrunner安装网盘地址:  http://pan.baidu.com/s/1hrP6mDQ 一般会提示:“vc2005_sp1_with_atl_fix_redist 2.确认时提示缺少 ...

  10. js中call、apply、bind的区别

    var Person = { name : 'alice', say : function(txt1,txt2) { console.info(txt1+txt2); console.info(thi ...