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. myeclipse实现Servlet实例(3) 通过继承HttpServlet接口实现

    (1) 在软件公司 90%都是通过该方法开发. //在HttpServlet 中,设计者对post 提交和 get提交分别处理   //回忆 <form action="提交给?&qu ...

  2. socket 发送Ping包

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

  3. gzcompress, gzencode, gzdeflate三个压缩函数的对比

    PHP的自带的函数中,有三个压缩相关的函数:gzcompress.gzencode.gzdeflate,下面我们通过一段程序,来比较一下这三个函数的压缩比.代码:$string = "8ae ...

  4. PHP本地域名解析教程

    1.找到C:\WINDOWS\system32\drivers\etc\hosts 127.0.0.1       localhost 127.0.0.1       www.zhosoft.com ...

  5. 数学之路(3)-机器学习(3)-机器学习算法-PCA

    PCA 主成分分析(Principal components analysis,PCA),维基百科给出一个较容易理解的定义:“PCA是一个正交化线性变换,把数据变换到一个新的坐标系统中,使得这一数据的 ...

  6. Android模仿jquery异步请求

    01 package com.xbl.task; 02   03 import java.io.BufferedReader; 04 import java.io.InputStream; 05 im ...

  7. Solr 安装与集成IK中文分词器

    创建wangchuanfu core 1.  在example目录下创建wangchuanfu-solr文件夹: 2.  将./solr下的solr.xml拷贝到wangchuanfu-solr目录下 ...

  8. JS--显示类型转换Number—隐式类型转换

    显示类型转换 (强制类型转换):Number()parseInt()parseFloat() Number是整体转换--能够把一个看起来像数字的字符串转成数字--尽量去转换能转的都转了 var a = ...

  9. 《第一行代码》学习笔记21-Git

    Git(1) 1.Git是一个开源的分布式版本控制工具,其开发者是Linux操作系统的作者Linus Torvalds. 2.仓库(Repository)是用于保存版本管理所需要信息的地方,所有本地提 ...

  10. SQLSERVER内核架构剖析 (转)

    我们做管理软件的,主要核心就在数据存储管理上.所以数据库设计是我们的重中之重.为了让我们的管理软件能够稳定.可扩展.性能优秀.可跟踪排错. 可升级部署.可插件运行,我们往往研发自己的管理软件开发平台. ...