POJ3070Fibonacci(矩阵快速幂+高效)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 11587 | Accepted: 8229 | 
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:
.
题意:就是求斐波那契数列,只是有点大,递推也会超时,矩阵快速幂0ms,搞定;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = ;
const int mod = ;
struct Mat
{
int mat[][];
};
Mat operator * (Mat a, Mat b)
{
Mat c;
memset(c.mat, , sizeof(c.mat));
for(int k = ; k < N; k++)
{
for(int i = ; i < N; i++)
{
for(int j = ; j < N; j++)
{
c.mat[i][j] = (c.mat[i][j] + a.mat[i][k] * b.mat[k][j] % mod) % mod;
}
}
}
return c;
}
Mat operator ^ (Mat a, int k)
{
Mat c;
for(int i = ; i < N; i++)
for(int j = ; j < N; j++)
c.mat[i][j] = (i == j);
while(k)
{
if(k & )
c = c * a;
a = a * a;
k >>= ;
}
return c;
}
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
if(n == -)
break;
if(n == )
printf("0\n");
else if(n == )
printf("1\n");
else
{
Mat a;
a.mat[][] = ;
a.mat[][] = ;
a.mat[][] = ;
a.mat[][] = ;
a = a ^ (n - );
printf("%d\n",a.mat[][]);
}
} return ;
}
POJ3070Fibonacci(矩阵快速幂+高效)的更多相关文章
- POJ-3070Fibonacci(矩阵快速幂求Fibonacci数列) uva 10689 Yet another Number Sequence【矩阵快速幂】
		
典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a) ...
 - nyoj_148_fibonacci数列(二)_矩阵快速幂
		
fibonacci数列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 In the Fibonacci integer sequence, F0 = 0, F ...
 - 矩阵快速幂小结-Hdu2604
		
矩阵快速幂可以想象为线性代数的矩阵相乘,主要是运用于高效的计算矩阵高次方. 将矩阵两两分组,若要求a^n,即知道a^(n/2)次方即可,矩阵快速幂便是运用的这个思路. 比方想求(A)^7那么(A)^6 ...
 - 整数快速乘法/快速幂+矩阵快速幂+Strassen算法
		
快速幂算法可以说是ACM一类竞赛中必不可少,并且也是非常基础的一类算法,鉴于我一直学的比较零散,所以今天用这个帖子总结一下 快速乘法通常有两类应用:一.整数的运算,计算(a*b) mod c 二.矩 ...
 - leetcode_935. Knight Dialer_动态规划_矩阵快速幂
		
https://leetcode.com/problems/knight-dialer/ 在如下图的拨号键盘上,初始在键盘中任意位置,按照国际象棋中骑士(中国象棋中马)的走法走N-1步,能拨出多少种不 ...
 - 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
		
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
 - 51nod 算法马拉松18 B 非010串 矩阵快速幂
		
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...
 - 51nod 1113 矩阵快速幂
		
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...
 - 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
		
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
 
随机推荐
- normal.1
			
11 # coding:utf-8 def maxnum(ipstr): ipstr = ipstr.split(' ')[1] return ipstr def minnum(ipstr): ips ...
 - JS 之BOM
			
BOM的核心对象是window,表示浏览器的一个实例. 使用框架时,window.top对象指顶层框架,也就是浏览器窗口.window.parent对象指包含当前窗口的框架,也就是父框架.window ...
 - Spring MVC实现文件下载
			
下载文件① 下载文件需要将byte数组还原成文件. 首先使用mybatis将数据库中的byte数组查出来,指定文件名(包括格式).然后使用OutputStream将文件输入 @RequestMapp ...
 - 用virtualenv管理python3运行环境
			
1. 简介 virtualenv可以用来管理互不干扰的独立python虚拟环境,在有些场景下非常有用,例如: 你有两个python项目,一个是python2.7的,另一个是python3的,可以创建两 ...
 - LeetCode:Remove Duplicates from Sorted Array I II
			
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
 - 信息安全系统设计基础实验一:Linux开发环境的配置和使用(20135234,20135229)
			
http://www.cnblogs.com/mqy123/p/4968386.html
 - 4.HBase In Action 第一章-HBase简介(1.1.2 数据创新)
			
As we now know, many prominent internet companies, most notably Google, Amazon, Yahoo!, and Facebook ...
 - 随便谈谈用canvas来实现文字图片粒子化
			
声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 看了岑安大大的教程http://www.cnblogs.com/hongru/archive/2012/03/28/2420415.htm ...
 - Promise 学习笔记 - 时间支配者
			
本文同步自我的个人博客:http://www.52cik.com/2015/11/08/promise.html JavaScript 的 promises 事实标准称为 Promises/A+.ES ...
 - 开源搜索 Iveely Search Engine 0.6.0 发布 -- 黎明前的娇嫩
			
快两年了,Iveely Search Engine已经走过了5个版本的岁月,虽出生“贫寒”,没有任何开源基金会的支持,没有优秀的“干爹.干妈”,它凭着它的爱好者的支持,0.6.0终于破壳而出,7年前, ...