POJ_Fibonacci POJ_3070(矩阵快速幂入门题,附上自己写的矩阵模板)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10521 | Accepted: 7477 |
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
typedef struct Matrix
{
// Made by xiper , Last updata : 2015 / 6 / 14
int r , c , ele[][];
Matrix(const int & r , const int & c)
{
this->r = r , this->c = c; // i will not init for ele , u should do it
}
friend ostream& operator << (ostream & os,const Matrix & x)
{
for(int i = ; i < x.r ; ++ i)
{
for(int j = ; j < x.c ; ++ j)
os << x.ele[i][j] << " ";
os << endl;
}
return os;
}
Matrix operator * (const Matrix & x) const
{
if (c != x.r)
{
cout << "Error on Matrix operator * , (c1 != r1)" << endl;
return Matrix(,);
}
Matrix res(r,x.c);
for(int i = ; i < r ; ++ i)
for(int j = ; j < x.c ; ++ j)
{
int sum = ;
for(int k = ; k < c ; ++ k)
sum += ele[i][k]*x.ele[k][j];
res.ele[i][j] = sum;
}
return res;
}
Matrix operator * (const int & x ) const
{
Matrix res(r,c);
for(int i = ; i < r ; ++ i)
for(int j = ; j < c ; ++ j)
res.ele[i][j] = ele[i][j]*x;
return res;
}
Matrix operator + (const Matrix & x) const
{
if (x.r != r || x.c != c)
{
cout << "Error on Matrix operator + , (r1 != r2 || c1 != c2)" << endl;
return Matrix(,);
}
Matrix res(r,c);
for(int i = ; i < r ; ++ i)
for(int j = ; j < c ; ++ j)
res.ele[i][j] = ele[i][j] + x.ele[i][j];
return res;
}
Matrix operator - (const Matrix & x) const
{
if (x.r != r || x.c != c)
{
cout << "Error on Matrix operator + , (r1 != r2 || c1 != c2)" << endl;
return Matrix(,);
}
Matrix res(r,c);
for(int i = ; i < r ; ++ i)
for(int j = ; j < c ; ++ j)
res.ele[i][j] = ele[i][j] - x.ele[i][j];
return res;
}
void r_ope(int whichr , int num)
{
for(int i = ; i < c ; ++ i)
ele[whichr][i] += num;
}
void c_ope(int whichc , int num)
{
for(int i = ; i < r ; ++ i)
ele[i][whichc] += num;
}
void init(int x)
{
for(int i = ; i < r ; ++ i)
for(int j = ; j < c ; ++ j)
ele[i][j] = x;
}
void init_dig()
{
memset(ele,,sizeof(ele));
for(int i = ; i < min(r,c) ; ++ i)
ele[i][i] = ;
}
Matrix Mulite (const Matrix & x ,int mod) const
{
if (c != x.r)
{
cout << "Error on Matrix function Mulite(pow may be) , (c1 != r1)" << endl;
return Matrix(,);
}
Matrix res(r,x.c);
for(int i = ; i < r ; ++ i)
for(int j = ; j < x.c ; ++ j)
{
int sum = ;
for(int k = ; k < c ; ++ k)
sum += (ele[i][k]*x.ele[k][j]) % mod;
res.ele[i][j] = sum % mod;
}
return res;
}
Matrix pow(int n , int mod)
{
if (r != c)
{
cout << "Error on Matrix function pow , (r != c)" << endl;
return Matrix(,);
}
Matrix tmp(r,c);
memcpy(tmp.ele,ele,sizeof(ele));
Matrix res(r,c);
res.init_dig();
while(n)
{
if (n & )
res = res.Mulite(tmp,mod);
n >>= ;
tmp = tmp.Mulite(tmp,mod);
}
return res;
}
};
AC代码就不贴了(其实就几行。。)
POJ_Fibonacci POJ_3070(矩阵快速幂入门题,附上自己写的矩阵模板)的更多相关文章
- hdu 1575 Tr A (矩阵快速幂入门题)
题目 先上一个链接:十个利用矩阵乘法解决的经典题目 这个题目和第二个类似 由于矩阵乘法具有结合律,因此A^4 = A * A * A * A = (A*A) * (A*A) = A^2 * A^2.我 ...
- 矩阵快速幂(入门) 学习笔记hdu1005, hdu1575, hdu1757
矩阵快速幂是基于普通的快速幂的一种扩展,如果不知道的快速幂的请参见http://www.cnblogs.com/Howe-Young/p/4097277.html.二进制这个东西太神奇了,好多优秀的算 ...
- HDU 1575 Tr A 【矩阵经典2 矩阵快速幂入门】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1575 Tr A Time Limit: 1000/1000 MS (Java/Others) Me ...
- HDU 1575 矩阵快速幂裸题
题意:中文题 我就不说了吧,... 思路:矩阵快速幂 // by SiriusRen #include <cstdio> #include <cstring> using na ...
- POJ3070矩阵快速幂简单题
题意: 求斐波那契后四位,n <= 1,000,000,000. 思路: 简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...
- 集训第六周 矩阵快速幂 K题
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- Foj1683矩阵快速幂水题
Foj 1683 纪念SlingShot 题目链接:http://acm.fzu.edu.cn/problem.php?pid=1683 题目:已知 F(n)=3 * F(n-1)+2 * F(n-2 ...
- LightOJ 1065 - Number Sequence 矩阵快速幂水题
http://www.lightoj.com/volume_showproblem.php?problem=1065 题意:给出递推式f(0) = a, f(1) = b, f(n) = f(n - ...
- hdu 2604 Queuing(矩阵快速幂乘法)
Problem Description Queues and Priority Queues are data structures which are known to most computer ...
随机推荐
- Codeforce 215 div1
C 把每个qi看成点,则问题转化为:求一个最大的k,遍历k个点的完全图需要的最小步数+1不超过n, (这里+1的原因是把起点加进去) 讨论k的奇偶: k为奇数,每个点度数为偶数,这是一个欧拉回路,步数 ...
- WPF - 使用Microsoft.Win32.OpenFileDialog打开文件,使用Microsoft.Win32.SaveFileDialog将文件另存
1. WPF 使用这个方法打开文件,很方便,而且可以记住上次打开的路径. Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.W ...
- 源码分析之struts1自定义方法的使用与执行过程
最近有人问我,你做项目中用户的一个请求是怎么与struts1交互的,我说请求的url中包含了action的名字和方法名,这样就可以找到相应方法,执行并返回给用户了. 他又问,那struts1中有什么方 ...
- JSF标签大全详解
1. JSF入门 藉由以下的几个主题,可以大致了解JSF的轮廓与特性,我们来看看网页设计人员与应用程序设计人员各负责什么. 1.1简介JSF Web应用程序的开发与传统的单机程序开发在本质上存在着太多 ...
- Android-它们的定义Dialog
Android-它们的定义Dialog 2014年4月27日 星期天 天气晴朗 心情平静 本篇博文来分享一个也是开发中常常须要用到的功能-自己定义对话框,这里我用到了Android中的图形资源shap ...
- CentOS7--DNS处理模块DnsPython的简单使用
初步了解: DnsPython是Python实现的一个DNS工具包,支持几乎所有的记录类型. 安装: # wget http://www.dnspython.org/kits/1.9.4/dnspyt ...
- 查看哪些ip破解你ssh密码以及次数
在互联网中,总有一些无聊的人,每天不断的猜解别人服务器的密码!作为linux服务器的管理员,我们应该了解哪些IP经常不断地扫描我们的SSH端口以尝试暴力破解,下面我们用一条命令简单列出哪些IP破解你S ...
- C#构造函数的 "继承" 问题
首先说明下 之所以用 双引号 是因为构造函数是没有继承的 派生类默认会调用基类的无参数构造函数 比如: public class A { public A() { Co ...
- [MVC4-基礎] 使用DataAnnotations+jQuery進行表單驗證
我目前有以下表單,Select部分因為必須上一層有選擇下層才有資料,因此使用jQuery驗證問題類型是否有選擇就好,而問題描述要驗證是否為空,這裡採用MVC內建的DataAnnotations來驗證. ...
- Sass@规则
@importSass 支持所有 CSS3 的 @ 规则, 以及一些 Sass 专属的规则,也被称为“指令(directives)”.Sass 扩展了 CSS 的 @import 规则,让它能够引入 ...