poj 3070 Fibonacci 矩阵快速幂
Description
In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
An alternative formula for the Fibonacci sequence is
.
Given an integer n, your goal is to compute the last 4 digits of Fn.
Input
The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.
Output
For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).
Sample Input
0
9
999999999
1000000000
-1
Sample Output
0
34
626
6875
Hint
As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by
.
Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix.
.
题解:
按题目要求很容易写出矩阵
F[n] 0 1 1
F[n-1] 0 1 0
直接上矩阵快速幂即可
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
const int mod=;
int n;
struct matrix
{
ll a[][];
matrix(){for(int i=;i<=;i++)for(int j=;j<=;j++)a[i][j]=;}
matrix(ll b[][]){for(int i=;i<=;i++)for(int j=;j<=;j++)a[i][j]=b[i][j];}
inline matrix operator *(matrix p){
matrix tmp;
for(int i=;i<=;i++)
for(int j=;j<=;j++){
tmp.a[i][j]=;
for(int k=;k<=;k++)
tmp.a[i][j]+=a[i][k]*p.a[k][j],tmp.a[i][j]%=mod;
}
return tmp;
}
};
ll work(){
if(n==)return ;
if(n==||n==)return ;
n-=;
ll t[][]={{,,},{,,},{,,}};ll sum[][]={{,,},{,,},{,,}};
matrix S=matrix(t),T=matrix(sum);
while(n){
if(n&)S=S*T;
T=T*T;n>>=;
}
return S.a[][];
}
int main()
{
while(scanf("%d",&n))
{
if(n==-)break;
printf("%lld\n",work());
}
return ;
}
poj 3070 Fibonacci 矩阵快速幂的更多相关文章
- poj 3070 Fibonacci (矩阵快速幂乘/模板)
题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...
- POJ 3070 Fibonacci 矩阵快速幂模板
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18607 Accepted: 12920 Descr ...
- POJ 3070 Fibonacci矩阵快速幂 --斐波那契
题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...
- HDU 1588 Gauss Fibonacci(矩阵快速幂)
Gauss Fibonacci Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- POJ——3070Fibonacci(矩阵快速幂)
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12329 Accepted: 8748 Descri ...
- UVA - 10229 Modular Fibonacci 矩阵快速幂
Modular Fibonacci The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 3 ...
- POJ 3744 【矩阵快速幂优化 概率DP】
搞懂了什么是矩阵快速幂优化.... 这道题的重点不是DP. /* 题意: 小明要走某条路,按照个人兴致,向前走一步的概率是p,向前跳两步的概率是1-p,但是地上有地雷,给了地雷的x坐标,(一维),求小 ...
- poj3070 Fibonacci 矩阵快速幂
学了线代之后 终于明白了矩阵的乘法.. 于是 第一道矩阵快速幂.. 实在是太水了... 这差不多是个模板了 #include <cstdlib> #include <cstring& ...
- poj 3735 稀疏矩阵矩阵快速幂
设人数为 $n$,构造 $(n + 1) \times (n + 1)$ 的矩阵 得花生:将改行的最后一列元素 $+ 1$ \begin{gather}\begin{bmatrix}1 & 0 ...
随机推荐
- Basic FIFO Queue
Queue - 一种线程安全的FIFO实现 Python的Queue模块提供一种适用于多线程编程的FIFO实现.它可用于在生产者(producer)和消费者(consumer)之间线程安全(threa ...
- git基本用法
基本用法(下) 一.实验说明 本节实验为 Git 入门第二个实验,继续练习最常用的git命令. 1.1 实验准备 在进行该实验之前,可以先clone一个练习项目gitproject ...
- 前端之bootstrap模态框
简介:模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等. Modal简介 Modal实现弹出表单 M ...
- kubernetes入门(07)kubernetes的核心概念(4)
一.pod 二.Volume volume可以为容器提供持久化存储,比如 三.私有镜像 在使用私有镜像时,需要创建一个docker registry secret,并在容器中引用.创建docker r ...
- Python进程
(先分享一个Python在线编程的网站http://www.pythontip.com/coding/skulpt-interactive/) (本文为原创作品,欢迎转载,转载请注明出处) 一.概念 ...
- R数据分析 第一篇:温习概率论
概率论是人们在长期实践中发现的理论,是客观存在的.自然界和社会上发生的现象是多种多样的,有一类现象,在一定条件下必然发生,称作确定性现象,而概率论研究的现象是不确定性现象,嗯嗯,醒醒,概率论研究的对象 ...
- poj 2945 Find the Clones
https://vjudge.net/problem/POJ-2945 题意: 给出n个长度相同的DNA序列,如果一个DNA序列出现过两次,那么就有说明它被复制了一次.问被复制0次,1次,2次--n- ...
- Eclipse中JavaSwing图形插件安装
1.在百度中搜索WindowBuilder,找到http://www.eclipse.org/windowbuilder/ 2.点击Download调转到页面: 因为我的eclipse版本是 3.点击 ...
- C语言 左值、右值
左值就是在赋值中可以放在赋值操作符两边的值 右值则是只可以放在赋值操作符右边的值 ++i是直接给i变量加1,然后返回i本身,因为i是变量,所以可以被赋值,因此是左值表达式i++现产生一个临时变量,记录 ...
- 框架学习笔记之Mybatis(一)
一.简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单 ...