POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 17171 | Accepted: 11999 |
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:
.
Source
#include <iostream>
#include <cstddef>
#include <cstring>
#include <vector> using namespace std; typedef long long ll;
const int mod=;
typedef vector<ll> vec;
typedef vector <vec> mat; mat mul(mat &a,mat &b)
{
mat c(a.size(),vec(b[].size()));
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++){
c[i][j]+=a[i][k]*b[k][j];
c[i][j]%=mod;
}
return c;
} mat Pow(mat a,ll n)
{
mat res(a.size(),vec(a.size()));
for(int i=;i<a.size();i++)
res[i][i]=;
while(n)
{
if(n&)
res=mul(res,a);
a=mul(a,a);
n/=;
}
return res;
} ll solve(ll n)
{
mat a(,vec());
a[][]=;
a[][]=;
a[][]=;
a[][]=;
a=Pow(a,n);
return a[][];
} int main()
{
ll n;
while(cin>>n&&n!=-)
{
cout<<solve(n)<<endl;
}
}
斐波那契快速幂
POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】的更多相关文章
- POJ 3070(求斐波那契数 矩阵快速幂)
题意就是求第 n 个斐波那契数. 由于时间和内存限制,显然不能直接暴力解或者打表,想到用矩阵快速幂的做法. 代码如下: #include <cstdio> using namespace ...
- POJ3070 斐波那契数列 矩阵快速幂
题目链接:http://poj.org/problem?id=3070 题意就是让你求斐波那契数列,不过n非常大,只能用logn的矩阵快速幂来做了 刚学完矩阵快速幂刷的水题,POJ不能用万能头文件是真 ...
- HDU4549 M斐波那契数列 矩阵快速幂+欧拉函数+欧拉定理
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- 51nod1242 斐波那契数列 矩阵快速幂
1242 斐波那契数列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 #include<stdio.h> #define mod 100000000 ...
- hdu4549 M斐波那契数列 矩阵快速幂+快速幂
M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F[n]的 ...
- hdu 4549 M斐波那契数列 矩阵快速幂+欧拉定理
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Problem ...
- 洛谷P1962 斐波那契数列(矩阵快速幂)
题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数) 题目描述 请 ...
- hdu 4549 M斐波拉契 (矩阵快速幂 + 费马小定理)
Problem DescriptionM斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在 ...
- 洛谷- P1306 斐波那契公约数 - 矩阵快速幂 斐波那契性质
P1306 斐波那契公约数:https://www.luogu.org/problemnew/show/P1306 这道题目就是求第n项和第m项的斐波那契数字,然后让这两个数求GCD,输出答案的后8位 ...
随机推荐
- 动态规划(入门,滚动数组,记录的都是状态):SWUSTACM-1010 魔兽争霸之最后的反击
题目: 1010: 魔兽争霸之最后的反击 Time Li ...
- Java的多态性Polymorphism
原文地址:http://www.cnblogs.com/jack204/archive/2012/10/29/2745150.html Java中多态性的实现 什么是多态 面向对象的三大特性:封装.继 ...
- HDU 3333 Turing Tree 莫队算法
题意: 给出一个序列和若干次询问,每次询问一个子序列去重后的所有元素之和. 分析: 先将序列离散化,然后离线处理所有询问. 用莫队算法维护每个数出现的次数,就可以一边移动区间一边维护不同元素之和. # ...
- Redis实现之字典
字典 字典,又称为符号表(symbol table).关联数组(associative array)或映射(map),是一种用于保存键值对(key-value pair)的抽象数据结构.在字典中,一个 ...
- Nhibernate官方体系结构图部分中文翻译
原文链接 :http://nhibernate.info/doc/nh/en/index.html#architecture 体系结构图 高度抽象NHibernate体系架构图 这幅图展示了NHibe ...
- Tensorflow实现Mask R-CNN实例分割通用框架,检测,分割和特征点定位一次搞定(多图)
Mask R-CNN实例分割通用框架,检测,分割和特征点定位一次搞定(多图) 导语:Mask R-CNN是Faster R-CNN的扩展形式,能够有效地检测图像中的目标,同时还能为每个实例生成一个 ...
- CentOS6.5创建yum源
昨天给布置个新的需求,做一个Yum仓库,要求是HTTP式的,在某个服务器上搭建个Yum仓库,能让其它的机器有了这个机器的.repo仓库文件后就可以从本地下载安装软件,以前都是下载后直接yum inst ...
- xml文件的生成
关于android中自定义xml文件的生成,请看示例代码(主要来源于黑马教程): import java.io.File; import java.io.FileNotFoundException; ...
- 并发编程——IO模型(6)
1.IO模型分类 同步IO #所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不会返回.按照这个定义,其实绝大多数函数都是同步调用.但是一般而言,我们在说同步.异步的时候,特指那些需要 ...
- CSU-2173 Use FFT
CSU-2173 Use FFT Description Bobo computes the product P(x)⋅Q(x)=\(c_0 + c_1x + - + c_{n+m}x^{n + m} ...