poj3070--Fibonacci(矩阵的高速幂)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9650 | Accepted: 6856 |
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 <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
struct node{
LL s11 , s12 , s21 , s22 ;
};
node f(node a,node b)
{
node p ;
p.s11 = (a.s11*b.s11 + a.s12*b.s21)%10000 ;
p.s12 = (a.s11*b.s12 + a.s12*b.s22)%10000 ;
p.s21 = (a.s21*b.s11 + a.s22*b.s21)%10000 ;
p.s22 = (a.s21*b.s12 + a.s22*b.s22)%10000 ;
return p ;
}
node pow(node p,int n)
{
node q ;
q.s11 = q.s22 = 1 ;
q.s12 = q.s21 = 0 ;
if(n == 0)
return q ;
q = pow(p,n/2);
q = f(q,q);
if( n%2 )
q = f(q,p);
return q ;
}
int main()
{
int n ;
node p ;
while(scanf("%d", &n) && n != -1)
{
p.s11 = p.s12 = p.s21 = 1 ;
p.s22 = 0 ;
p = pow(p,n);
printf("%d\n", p.s12);
}
return 0;
}
poj3070--Fibonacci(矩阵的高速幂)的更多相关文章
- poj3070 Fibonacci 矩阵快速幂
学了线代之后 终于明白了矩阵的乘法.. 于是 第一道矩阵快速幂.. 实在是太水了... 这差不多是个模板了 #include <cstdlib> #include <cstring& ...
- POJ3070:Fibonacci(矩阵快速幂模板题)
http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdl ...
- POJ3070 Fibonacci[矩阵乘法]
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13677 Accepted: 9697 Descri ...
- POJ3070 Fibonacci[矩阵乘法]【学习笔记】
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13677 Accepted: 9697 Descri ...
- HDU2256-Problem of Precision(矩阵构造+高速幂)
pid=2256">题目链接 题意:求sqrt(sqrt(2) + sqrt(3)) ^ 2n MOD 1024 思路: 代码: #include <iostream> # ...
- HDU1588-Gauss Fibonacci(矩阵高速幂+等比数列二分求和)
题目链接 题意:g(x) = k * x + b.f(x) 为Fibonacci数列.求f(g(x)),从x = 1到n的数字之和sum.并对m取模. 思路: 设A = |(1, 1),(1, 0) ...
- HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)
HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出 ...
- hdu 3306 Another kind of Fibonacci(矩阵高速幂)
Another kind of Fibonacci Time Limit: 3000/10 ...
- UVA10518 - How Many Calls?(矩阵高速幂)
UVA10518 - How Many Calls?(矩阵高速幂) 题目链接 题目大意:给你fibonacci数列怎么求的.然后问你求f(n) = f(n - 1) + f(n - 2)须要多少次调用 ...
随机推荐
- 【Android基础】点击Back键退出应用程序
//第一种方法(弹出对话框) @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-genera ...
- java提高篇(十三)-----字符串
可以证明,字符串操作是计算机程序设计中最常见的行为. 一.String 首先我们要明确,String并不是基本数据类型,而是一个对象,并且是不可变的对象.查看源码就会发现String类为f ...
- JS达到Web指定保存的和打印功能的内容
背景 首先,说说文章的背景.近期手中的一个项目,因为需求中要求提供Web界面的打印功能.当然假设没有打印机,还能够提供保存到本地.项目组长把这个"小任务"分给了我.本着努力为组长分 ...
- JAVA学习(二):JDK介绍及其结构、用途
JDK介绍及其结构.用途 1.JDK的介绍 JDK是Java Development Kit 的缩写.是Sun Microsystems针对Java开发员的产品.它由一个处于操作系统层之上的执行环境还 ...
- 建立ORACLE10G DATA GUARD--->Physical Standby
下面是我自己建Physical Standby,按照下面的步骤一步我一步,当然,打造成功,以下步骤可以作为建筑物Data Guard结构操作手册. HA和DG差额:HA:可以做IP切换自己主动 DG ...
- 利用Ring Buffer在SQL Server 2008中进行连接故障排除
原文:利用Ring Buffer在SQL Server 2008中进行连接故障排除 出自:http://blogs.msdn.com/b/apgcdsd/archive/2011/11/21/ring ...
- 人迹罕至的android要完全退出程序的一种方法
最近的一个项目,无意中发现了一个方法,使android要完全退出程序的一种方法,遥想当年,以便找到让的有效途径android遇险完全退出程序,我不由得有些感慨. 在这里,不敢独享.和大家分享一下,还启 ...
- Windows命令行命令集锦
原文:Windows命令行命令集锦 转自:http://www.me2wg.com/bbs/forum.php?mod=viewthread&tid=15830 winver--------- ...
- bootstrap+jQuery.validate
bootstrap+jQuery.validate表单校验 谈谈表单校验 这大概是一种惯例,学习前台后台最开始接触的业务都是用户注册和登录.现在社会坚持以人为本的理念,在网站开发过程同样如此.Us ...
- android sdk linux 文本 64 位置
android sdk linux 64 之前获得模.现在补上一,然后,小伙伴们下载 版权声明:本文博主原创文章,博客,未经同意不得转载.