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 ...
随机推荐
- zTree第四章,异步加载,后端
后端 ------------------------------------------------------------------------------------------------- ...
- [Swift实际操作]七、常见概念-(12)使用DispatchGroup(调度组)管理线程数组
本文将为你演示调度组的使用,使用调度组可以将多个线程中的人物进行组合管理,可以设置当多个相同层次的任务完成之后,再执行另一项任务. 首先导入需要使用的界面工具框架 import UIKit 在控制台输 ...
- find查找文件命令 - Linux系统中的常用技巧整理
“find”在Linux系统中是比较常用的文件查找命令,使用方法有很多,可以拥有查找文件.文件目录.文件更新时间.文件大小.文件权限及对比文件时间.下面是整理的“find”常用方法,方便以后需要的时候 ...
- UITableView 头部效果/放大/移动跟随效果
[self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOp ...
- tomcat startup.brt启动一闪而过
今天启动皕杰自带的tomcat的时候,直接点startup.brt控制台窗口一闪而过. 改用cmd命令启动: 切换到 E:\皕杰报表\皕杰报表3.5\DemoServer\bin目录 运行startu ...
- Vue局部注册 或者全局注册 组件时,组件定义要用 分隔命名,用驼峰命名是不生效的
Vue.component('all-canuse',{ props:['message'], template:'<div>{{message}}</div>' }) 像这样
- MVC3权限验证,诡异的OnAuthorization
mvc3权限验证 protected override void OnAuthorization(AuthorizationContext filterContext) { if (//开始权限验证返 ...
- Java数组和各种List的性能比较
以下程序分别对Java数组.ArrayList.LinkedList和Vector进行随机访问和迭代等操作,并比较这种集合的性能. package cn.lion.test; public class ...
- Zepto自定义模块打包构建
文章转自 http://www.chengxuyuans.com/web_technology/zeptojs-build.html zepto.js 是个好东西,遵循 jQuery API,但比 j ...
- C 标准库 - string.h之memcpy使用
memcpy Copy block of memory Copies the values of num bytes from the location pointed to by source di ...