Fibonacci
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(矩阵快速幂入门题,附上自己写的矩阵模板)的更多相关文章

  1. hdu 1575 Tr A (矩阵快速幂入门题)

    题目 先上一个链接:十个利用矩阵乘法解决的经典题目 这个题目和第二个类似 由于矩阵乘法具有结合律,因此A^4 = A * A * A * A = (A*A) * (A*A) = A^2 * A^2.我 ...

  2. 矩阵快速幂(入门) 学习笔记hdu1005, hdu1575, hdu1757

    矩阵快速幂是基于普通的快速幂的一种扩展,如果不知道的快速幂的请参见http://www.cnblogs.com/Howe-Young/p/4097277.html.二进制这个东西太神奇了,好多优秀的算 ...

  3. HDU 1575 Tr A 【矩阵经典2 矩阵快速幂入门】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Me ...

  4. HDU 1575 矩阵快速幂裸题

    题意:中文题 我就不说了吧,... 思路:矩阵快速幂 // by SiriusRen #include <cstdio> #include <cstring> using na ...

  5. POJ3070矩阵快速幂简单题

    题意:       求斐波那契后四位,n <= 1,000,000,000. 思路:        简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...

  6. 集训第六周 矩阵快速幂 K题

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

  7. Foj1683矩阵快速幂水题

    Foj 1683 纪念SlingShot 题目链接:http://acm.fzu.edu.cn/problem.php?pid=1683 题目:已知 F(n)=3 * F(n-1)+2 * F(n-2 ...

  8. LightOJ 1065 - Number Sequence 矩阵快速幂水题

    http://www.lightoj.com/volume_showproblem.php?problem=1065 题意:给出递推式f(0) = a, f(1) = b, f(n) = f(n - ...

  9. hdu 2604 Queuing(矩阵快速幂乘法)

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

随机推荐

  1. windows环境下nutch2.x 在eclipse中实现抓取数据存进mysql详细步骤

    nutch2.x 在eclipse中实现抓取数据存进mysql步骤 最近在研究nutch,花了几天时间,也遇到很多问题,最终结果还是成功了,在此记录,并给其他有兴趣的人提供参考,共同进步. 对nutc ...

  2. jQuery的ajax jsonp跨域请求

    了解:ajax.json.jsonp.“跨域”的关系 要弄清楚以上ajax.json.jsonp概念的关系,我觉得弄清楚ajax是“干什么的”,“怎么实现的”,“有什么问题”,“如果解决存在的问题”等 ...

  3. AssemblyInfo.cs文件的作用

    在asp.net中有一个配置文件AssemblyInfo.cs主要用来设定生成的有关程序集的常规信息dll文件的一些參数,以下是默认的AssemblyInfo.cs文件的内容详细介绍 //是否符合公共 ...

  4. OpenMp 基本

      OpenMp是由OpenMP Architecture Review Board牵头提出的,并已被广泛接受的,用于共享内存并行系统的多线程程序设计的一套指导性的编译处理方案(Compiler Di ...

  5. ORA-24324、ORA-12560、ORA-12514

    SQL> startup ERROR: ORA-24324: 未初始化服务句柄 ORA-01041: 内部错误, hostdef 扩展名不存在. SQL> conn sys /nolog; ...

  6. 如何让div横向排列

    方法一: 一般情况,默认的div是写一个换一行,那么如何定义两个div横向排列而不换行呢? div默认的display属性是block.所以每一个div都是新的一行,现在把display换成inlin ...

  7. compass安装

    修改ruby软件包的sources 国外服务器不给力,经常链接失败,换成国内淘宝的:https://ruby.taobao.org/ 先移除本有的sources gem sources --remov ...

  8. C# XML 根级别上的数据无效

    XmlDocument加载xml方法 XmlDocument doc = new XmlDocument(); //加载xml 字符串 doc.LoadXml(_Store); //加载xml文件 d ...

  9. Asp.Net中JSON的序列化和反序列化-----JavaScriptSerializer ,加上自己工作心得

    在工作中和手机通信用到web服务和javascriptSerializer,返回json数据,供手机端调用,一开始返回的数据是一大堆,比如 [{"word_picture9":&q ...

  10. Easyui 排序时 自动向后排传sort order 你妹真坑爹

    request 的时候 发现 sort 竟然是个数组!