POJ 3070 Fibonacci
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 矩阵快速幂入门题,构造矩阵[fn,fn-1]*[1,1]在自己敲一遍模板就行了。
[0 ,0 ] [1,0]
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
typedef long long int LL;
using namespace std; int n,mod=; struct Matrix
{
int a[][];
Matrix(){memset(a,,sizeof(a));}
Matrix operator* (const Matrix &p)
{
Matrix res;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
res.a[i][j]+=(a[i][k]*p.a[k][j]%mod);
}
res.a[i][j]%=mod;
}
}
return res;
}
}ans,base; Matrix quick_pow(Matrix base,int k)
{
Matrix res;
for(int i=;i<;i++)
{
res.a[i][i]=;
}
while(k)
{
if(k&) res=res*base;
base=base*base;
k>>=;
}
return res;
} void init_Matrix()
{
ans.a[][]=;
ans.a[][]=;
ans.a[][]=;
ans.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
} int main()
{
while(scanf("%d",&n)&&n!=-)
{
init_Matrix();
if(n==) printf("0\n");
else if(n==) printf("1\n");
else
{
ans=ans*quick_pow(base,n-);
printf("%d\n",ans.a[][]);
}
}
return ;
}
POJ 3070 Fibonacci的更多相关文章
- 矩阵快速幂 POJ 3070 Fibonacci
题目传送门 /* 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 */ #include <cstdio> #include <algori ...
- POJ 3070 Fibonacci(矩阵高速功率)
职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...
- 矩阵经典题目六:poj 3070 Fibonacci
http://poj.org/problem?id=3070 按已构造好的矩阵,那么该矩阵的n次方的右上角的数便是f[n]. #include <stdio.h> #include < ...
- 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 【矩阵快速幂】
<题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...
- poj 3070 Fibonacci 矩阵相乘
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7715 Accepted: 5474 Descrip ...
- POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17171 Accepted: 11999 Descr ...
- 题解报告:poj 3070 Fibonacci
题目链接:http://poj.org/problem?id=3070 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, a ...
随机推荐
- 带你玩转JavaWeb开发之三 -JS插件实战开发
前提:需要掌握的知识点 填写HTML代码 Element元素中有一个innerHTML属性,这个属性可以填写一段html代码 innerHTML = "<font ...
- VS2012 提示未找到与约束 ContractName 匹配的倒出
问题描述: 今天由于开发需要,我安装了一下 Vs 2015 按照之后 打开原来的项目 vs2012 打开,点击类文件提示错误 未找到与约束 ContractName 匹配的倒出 解决办法: 1 去微 ...
- 使用HTML5技术控制电脑或手机上的摄像头
移动设备和桌面电脑上的客户端API起初并不是同步的.最初总是移动设备上先拥有某些功能和相应的API,但慢慢的,这些API会出现在桌面电脑上.其中一个应用接口技术就是getUserMedia API,它 ...
- 分享 rabbitMQ入门详解
原文地址http://blog.csdn.net/cugb1004101218/article/details/21243927 目录(?)[-] rabbitMQ说明文档 rabbitMQ是什么 消 ...
- Oracle之nclob类型
此类型会严重影响查询效率,请少用: nclob字段在查询结果中显示为<NCLOB>,查看nclob类型的值方法有两种 a.可点开...查看具体数据 b.选择所有数据,右击复制到Excel, ...
- C#异常处理性能测试
异常处理性能测试 using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq ...
- Hibernate操作指南-实体之间的关联关系(基于注解)
- NuGet控制台有几个常用命令
NuGet控制台有几个常用命令 Get-Package 获取当前项目已经安装的类库 Install-Package 安装指定类库,命令格式如下:Install-Package 类库ID,如Instal ...
- 通过pustil模块取pid及对应的pidname
通过pustil模块取pid及对应的pidname import psutil import json def getpid(): reslut = psutil.pids() return resl ...
- simple_tag,filte,分页以及cookie和装饰器
自定义simple_tag 内置的方法 首先Django中包含了很多内置的方法: 这里通过lower实现 在views视图函数中写如下代码: def tp3(request): name= " ...