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. MVC分部试图传参数

      @Html.Partial("_NavMenuPage", new ViewDataDictionary { { "proimshowId",imshowI ...

  2. Cracking the coding interview 第二章问题及解答

    文章的缘由可以参考此篇文章.目前完成了第二章,代码放在github上,地址在此.问题的描述都在对应的代码文件中.其他的章节仍在在进行中. 如果代码有问题,欢迎指正,谢谢. yetuweiba

  3. HDU4821---字符串hash,map判重

    这是2013年长春区域赛的铜牌题...然而第一次做的时候一直觉得会超时的..最后才知道并没有想象中的那么恐怖: 这题有两个注意的地方: (1)h[i] = h[i-1] * seed + s[i] - ...

  4. socket 发送Ping包

    参考链接: http://blog.csdn.net/zpxili/article/details/11542041 http://blog.csdn.net/cbuttonst/article/de ...

  5. iOS 部分问题总结2 - 苹果审核篇

    iOS 部分问题总结(二) - 苹果审核篇 1. 记录下5.1新规后上传被拒的问题排查和解决过程. 几天前,最新一次的更新被拒了,提示Invaild Binary.好在苹果同时发来了说明邮件做了详细说 ...

  6. 【hoj】1604 cable master

    简单,二分法,可是在oj上交的时候会有精度的限制,所以仅仅能把数扩得大一点,并且在扩的时候为防止尾数会自己主动生成一些非零数,所以还是自己吧扩到的位置设置为0,还有输出时由于%.2lf会自己有4设5入 ...

  7. VC6.0建立控制台程序实现PDA应用

    作者:iamlaosong 由于须要,又写起了文本界面的程序,以便PDA通过telnet连上运行. 假设是Linuxserver的话.这是非常easy的事,但是用户server是windows ser ...

  8. Android中自定义ActionBar的背景色等样式style

    Android中想要去自定义ActionBar的背景色等样式. [折腾过程] 1.自己找代码,发现对应的配置的地方了: AndroidManifest.xml ? 1 2 <applicatio ...

  9. 代码实现Layout android:layout_alignParentRight

    代码实现Layout android:layout_alignParentRight 例如: android:id="@+id/account_option" android:la ...

  10. .net学习体会

    莫名其妙学了IT,在课堂上学了C,C++,自学了C#,也做了一些网站项目,学习过程,写了厚厚的几本笔记本,却没写博文的习惯,前几天,有同学问学习.net的建议.其实我懂的也不多,也给了一些个人见解,主 ...