poj 3070 Fibonacci 矩阵相乘
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7715 | Accepted: 5474 |
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<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring> struct node
{
int a[][];
}now,cur; void make_first()
{
now.a[][]=;
now.a[][]=;
now.a[][]=;
now.a[][]=; cur.a[][]=;
cur.a[][]=;
cur.a[][]=;
cur.a[][]=;
} struct node make_cheng(node a,node b) //发现结构体忘记了。
{
struct node f;
int i,k,j;
memset(f.a,,sizeof(f.a));
for(i=;i<=;i++)
for(k=;k<=;k++)
if(a.a[i][k])
{
for(j=;j<=;j++)
if(b.a[k][j])
{
f.a[i][j]+=a.a[i][k]*b.a[k][j];
if(f.a[i][j]>=)
f.a[i][j]%=;
}
}
return f;
} void make_EF(int n)
{
make_first();
while(n)
{
if(n&)
{
now=make_cheng(now,cur);//!!!
}
n=n/;
cur=make_cheng(cur,cur);
}
printf("%d\n",now.a[][]);
} int main()
{
int n;
while(scanf("%d",&n)>)
{
if(n==-)break;
if(n==){printf("0\n");continue;}
if(n==||n==){printf("1\n");continue;}
make_EF(n-);
}
return ;
}
poj 3070 Fibonacci 矩阵相乘的更多相关文章
- POJ 3070 Fibonacci(矩阵高速功率)
职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...
- poj 3070 Fibonacci (矩阵快速幂乘/模板)
题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...
- poj 3070 Fibonacci 矩阵快速幂
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- POJ 3070 Fibonacci 矩阵高速求法
就是Fibonacci的矩阵算法.只是添加一点就是由于数字非常大,所以须要取10000模,计算矩阵的时候取模就能够了. 本题数据不强,只是数值本来就限制整数,故此能够0ms秒了. 以下程序十分清晰了, ...
- POJ 3070 Fibonacci 矩阵快速幂模板
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18607 Accepted: 12920 Descr ...
- POJ 3070 Fibonacci矩阵快速幂 --斐波那契
题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...
- 矩阵快速幂 POJ 3070 Fibonacci
题目传送门 /* 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 */ #include <cstdio> #include <algori ...
- POJ 3070 Fibonacci(矩阵快速幂)
题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...
- 题解报告:poj 3070 Fibonacci
题目链接:http://poj.org/problem?id=3070 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, a ...
随机推荐
- vue二级路由跳转后外部引入js失效问题解决方案
vue路由可以通过children嵌套,于是可以形成二级路由等等... 案例如下: routes: [ { path: '/', name: 'dy', component: dy, children ...
- Libra的第一天
wuli乖乖,今天是你降生第一天,以后的几年可能我们都会朝夕相处,你的成长就是我的向上,一起加油吧
- Python unittest第一篇:基础入门+命令行编译
unittest单元测试框架最初受JUnit启发,与其他语言的主要单元测试框架具有相似的风格. 它支持测试自动化,支持开启或关闭某个测试,支持结合测试.另外它可以生成各个单元测试的报告.为了实现以上功 ...
- Python抓取远程文件获取真实文件名
用urllib下载远程文件并转存到hdfs服务器,在下载时,下载地址中不一定包含文件名,需要从连接信息中获取. 1 file_url = request.form.get('file_url') 2 ...
- 在windows上部署使用Redis--资料整理
声明:一下只是针对windows系统,其他系统资料需自己补全. 很简单:下载.安装.安装桌面管理工具.测试.细不具表,下面几个网址应该足以解决你的所有问题. 网址访问专用Host: http://pa ...
- Windows2003终端服务器超出了最大连接数的问题解决方案
一.重启服务器.(将现有的连接都释放) 二.修改组策略 运行--gpedit.msc--计算机配置--管理模板--Windows组件--终端服务---右边“限制连接数量” 设定“已启用” --- ...
- Java多线程(二)同步与等待唤醒
1:数据安全问题 1.1:什么情况下会出现数据安全问题? 多个线程对同一个资源进行操作,并且操作资源的语句有多条.那么这个时候这些语句因为cpu的随机性,有可能被多个线程分开执行.导致数据安全问题. ...
- Ubuntu14.04下Cloudera安装搭建部署大数据集群(图文分五大步详解)(博主强烈推荐)(在线或离线)
第一步: Cloudera Manager安装之Cloudera Manager安装前准备(Ubuntu14.04)(一) 第二步: Cloudera Manager安装之时间服务器和时间客户端(Ub ...
- JavaScript设计模式-17.装饰者模式(下)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- eclipse 配置mapreduce环境出错
初学mapreduce,想在eclipse上配置mapreduce的环境,网上之类的教程,很多但是按照教程配之后,并不能正常运行. 碰到下面的错误: 15/10/17 20:10:39 INFO jv ...