poj 3070
Fibonacci
Time Limit: 1000 MS Memory Limit: 65536 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
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:
.
#include <iostream>
#include <string.h>
#include <stdio.h> using namespace std;
#define MOD 10000
int n; struct mat
{
int at[][];
} d; mat momu(mat a,mat b)
{
mat c;
memset(c.at,,sizeof(c.at));
for(int i=; i<n; i++)
{
for(int k=; k<n; k++)
{
if(a.at[i][k])
for(int j=; j<n; j++)
{
c.at[i][j]+=a.at[i][k]*b.at[k][j];
if(c.at[i][j]>MOD)
{
c.at[i][j]%=MOD;
}
}
}
}
return c;
} mat expo(mat p,int k)
{
if(k==)
return p;
mat e;
memset(e.at,,sizeof(e.at));
for(int i=; i<n; i++)
e.at[i][i]=;
if(k==)
return e;
while(k)
{
if(k&)
e=momu(p,e);
p=momu(p,p);
k>>=;
}
return e; } int main()
{
n=;
d.at[][]=;
d.at[][]=;
d.at[][]=;
d.at[][]=;
int t;
while(scanf("%d",&t)!=EOF)
{
if(t==-)
break;
mat dd=expo(d,t);
int ans=dd.at[][]%MOD;
printf("%d\n",ans);
}
return ;
}
约等于模板啦啦啦啦~~~~~~~~~~~
poj 3070的更多相关文章
- 矩阵快速幂 POJ 3070 Fibonacci
题目传送门 /* 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 */ #include <cstdio> #include <algori ...
- POJ 3070 Fibonacci(矩阵高速功率)
职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...
- poj 3070 && nyoj 148 矩阵快速幂
poj 3070 && nyoj 148 矩阵快速幂 题目链接 poj: http://poj.org/problem?id=3070 nyoj: http://acm.nyist.n ...
- POJ 3070 + 51Nod 1242 大斐波那契数取余
POJ 3070 #include "iostream" #include "cstdio" using namespace std; class matrix ...
- 矩阵经典题目六:poj 3070 Fibonacci
http://poj.org/problem?id=3070 按已构造好的矩阵,那么该矩阵的n次方的右上角的数便是f[n]. #include <stdio.h> #include < ...
- (矩阵快速幂) Fibonacci -- poj -- 3070
链接: http://poj.org/problem?id=3070 Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- poj 3070 矩阵计算Fibonacci
地址 http://poj.org/problem?id=3070 大意是输入一个数字 输出位于Fibonacci数列该位置的数字模10000的结果 由于n比较大 0 ≤ n ≤ 1,000,000, ...
- 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数列第n项 #include "iostream" #include "vector" #include "cstring& ...
随机推荐
- 使用guava进行对字符串的加锁
java的synchronized关键字是堆某对象加锁,但是我们当需要对某个字符串加锁怎么办 比如对同一个订单只能有一个操作,但是对其他订单的操作不影响 使用 guava包下的 Interner 类 ...
- java中关于正则一些基础使用
希望能帮到有需要的朋友.-----转载请注明出处. 对于正则处理相关的知识,我一开始是从网上找资料配合使用Java API1.6的一个中文版进行学习,很感谢翻译这个版本的团队(机构)或者个人,很感谢那 ...
- HTTP Client工具类
HTTP Client工具类: import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.ht ...
- ReportViewer内存泄漏问题解决方案[上]
做这个项目有点倒霉,快要验收的时候,发现微软ReportViewer控件的一个bug,导致我们的项目无法正常验收. 问题描叙:用ReportViewer本地模式做的报表,在ASP.NET页面中呈现.在 ...
- 史上最强防火墙iptables
#1.清空所有的防火墙规则 iptables -F iptables -X iptables -Z iptables -t NAT -F iptables -t NAT -X iptables -t ...
- C#实现:给定任意要给字符串,输出所有可能的回文的子字符串集合。
class Program { static void Main(string[] args) { string testStr = "sdfadfdsfadfdsfsdf"; i ...
- PHP 实现单一入口 apache配置
在apache的httpd.conf加入,需要把LoadModule rewrite_module modules/mod_rewrite.so前面的“#”去掉 DocumentRoot / < ...
- IOS 6和 IOS7适配的一些问题
由于在做一个ios的通用设计平台,那么客户端解析的时候就涉及到一些ios不同版本,不同分辨率的适配问题 首先碰到的就是navigation bar中的item的背景色的问题 在ios7中设置setti ...
- Zepto 实现checkbox全选与全不选状态切换
最近项目里用到foundation,而foundation4默认集成了Zepto,很多轮子要重造,所以有了下面的代码. <script> /** * Muti-Checking-Toggl ...
- Centos 7下mysql的安装与配置
1,先下载好MySQ安装包并解压(不做详细说明). 2,上传解压后的MySQL安装包到虚拟机上. 3,安装MySQL [root@localhost /]# yum install mysql5.7. ...