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& ...
随机推荐
- iOS 10 开发 相机相关的适配
升级 iOS 10 之后目测坑还是挺多的,记录一下吧,看看到时候会不会成为一个系列. 直入正题吧 今天在写 Swift 3 相关的一个项目小小练下手,发现调用相机,崩了.试试看调用相册,又特么崩了.然 ...
- discuz二次开发,分析和实现 之 向dz数据库插入自己的帖子吧
发个博客太麻烦了,难怪写博客的越来越少,吐一下,cnblogs的编辑器模板太丑! 最近开发社区 需要采集一些数据,使得模板输出有图文效果.就写了个简单的采集脚本,爬取目标站的内容,(用php 下载图片 ...
- My安卓知识5--百度地图api的使用,周边信息检索
虽然查了很多资料,但是这个问题还是解决不了,不知道为什么检索城市内的相关信息能用,检索周边信息语句就是用不了.代码如下,第一段是检索保定市内的加油站,第二段是检索周边的加油站.centerToMyLo ...
- Web程序的运行原理及流程(二)
其实WEB服务器和WEB应用服务器这两个概念特别容易混淆 可以理解为装了不同软件(服务)的两台计算机(服务器)吧 先对两个概念做一个简单介绍 了解了基本的概念 我们再用两个典型的例子做一下比较(建立 ...
- java问卷
1.你对自己的未来有什么规划?做了那些准备? 对于每个人来讲只有一次,七八十年的时间是一个即漫长然而又很短暂的过程,对于漫长与短暂的看法,由于每个人所处的环境的不同以及对人生看法的不同而有所差异.痛苦 ...
- 陨石坑之webapi使用filter
首先为什么说这是一个坑,是因为我们在webapi中使用filter的时候也许会先百度一下,好吧,挖坑的来了,我看了好几篇文章写的是使用System.Web.Mvc.Filters.ActionFilt ...
- C#中的using和yield return混合使用
最近写代码为了为了省事儿用了几个yield return,因为我不想New一个List<T>或者T[]对象再往里放元素,就直接返回IEnumerable<T>了.我的代码里还有 ...
- Mysql 分段统计
今天遇到个小问题觉得挺有意思,与大家分享. 需求是这样的,对数据库中的一张表做按时间的分段统计,结果只要每个区间的数量. select YEAR(create_time) as nian,MONTH( ...
- 扩大a标签的响应区域
<a href="" style="display:inline-block; width: 100%;">xx</a>
- python 实现彻底删除文件夹和文件夹下的文件
python 中有很多内置库可以帮忙用来删除文件夹和文件,当面对要删除多个非空文件夹,并且目录层次大于3层以上时,仅使用一种内置方法是无法达到彻底删除文件夹和文件的效果的,比较low的方式是多次调用直 ...