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)须要多少次调用 ...
随机推荐
- Swing动画之游戏角色
一.动画效果:实现了飞机飞行的动画效果,也实现了飞机的移动. 二.实现原理: 1.飞机飞行 的效果:事实上也还是重写paintComponent,依照一定的时间间隔更换图片就有了飞行的效果,动画就是更 ...
- SELECT 场 FROM 表 WHERE 字段 Like 条件
间有关的条件,SQL它提供了四种匹配模式: 1.%: 表示随意0个或多个字符.可匹配随意类型和长度的字符.有些情况下若是中文,请使用两个百分号(%%)表示. 比方 SELECT * FROM [use ...
- HSV 量化
function L=hsvquan(hsv) %对HSV量化,该3维特征矢量: h=hsv(:,:,1); s=hsv(:,:,2); v=hsv(:,:,3); % 假设对HSV 空间进行适当的量 ...
- oracle 创建字段自增长——两种实现方式汇总(转)
mysql等其他数据库中有随着记录的插入而表ID自动增长的功能,而oracle却没有这样的功能,我们有以下两种方式可以解决字段自增长的功能. 因为两种方式都需要通过创建序列来实现,这里先给出序列的创建 ...
- 创建在SQLServer 和 Oracle的 DBLINK
dblink 当我们要跨本地数据库.訪问另外一个数据库表中的数据时,本地数据库中就必需要创建远程数据库的dblink,通过dblink本地数据库能够像訪问本地数据库一样訪问远程数据库表中的数据. 一 ...
- hdoj 2063 过山车 【双边匹配匈牙利算法】
过山车 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- NET 中的多线程
NET 中的多线程 为什么使用多线程 使用户界面能够随时相应用户输入 当某个应用程序在进行大量运算时候,为了保证应用程序能够随时相应客户的输入,这个时候我们往往需要让大量运算和相应用户输入这两个行为在 ...
- 在CMD命令行和PowerShell中实现复制粘贴功能
在CMD命令行和PowerShell中实现复制粘贴功能 常常使用命令行或者PowerShell的朋友肯定会遇到这样的情况:粘贴文本非常easy,右键--选择粘贴就可以,可是想要复制命令 ...
- 【MongoDB】Serveral common command of MongoDb
In the recent days, since the overwork made me exhaused, on arrival to home I will go to bed, which ...
- 《UNIX级别编程环境》注意读出信号(2)
1.功能sigaction sigaction动与指定信号相关联的处理动作.其函数原型例如以下: #inlcude <signal.h> int sigaction(int signo,c ...