POJ——3070 Fibonacci (矩阵快速幂求fibonacci)

本文介绍了一种高效的算法,用于计算斐波那契数列中特定位置的数值,特别关注于大整数场景下,通过矩阵快速幂的方法避免了传统递归算法的效率瓶颈。该算法利用矩阵乘法的性质,能够快速求得斐波那契数列中任一位置的值,并只保留最后四位数字。

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 Fnare 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:

.

题意:求第几项的菲波那切数。

题解:因为数据大所以需要矩阵快速幂. 递推矩阵请看代码:

import java.util.*;
public class Main {
	static Scanner cin = new Scanner(System.in);
	static class hh{
		long [][] ma = new long [2][2];
		public hh() {
			for (int i = 0; i < 2;i++) {
				for (int j = 0; j < 2;j++) {
					ma[i][j]=0;
				}
			}
		}
	}
	static hh mul(hh a, hh b,long x) {
		hh c = new hh();
		for (int i = 0; i < 2;i++) {
			for (int j = 0; j < 2;j++) {
				for (int k = 0;k < 2;k++) {
					c.ma[i][j]=(c.ma[i][j]+a.ma[i][k]*b.ma[k][j]%x)%x;
				}
			}
		}
		return c;
	}
	static hh quick(hh a,long n,long c) {//模仿数的矩阵快速幂
		hh tmp = new hh();
		for (int i = 0; i < 2;i++) tmp.ma[i][i]=1;
		while(n!=0) {
			if(n%2==1) tmp=mul(tmp,a,c);
			n/=2;
			a=mul(a,a,c);
		}
		return tmp;
	}
	public static void main(String[] args){
		long t;
		while(cin.hasNext()) {
			t=cin.nextLong();
			if(t==-1) break;
			hh a = new hh();
			hh b = new hh();
			a.ma[0][0]=1;//递推矩阵构建
			a.ma[0][1]=1;
			a.ma[1][0]=1;
			a.ma[1][1]=0;
			b=quick(a,t,10000);
			System.out.println(b.ma[1][0]%10000);
		}
	}
}

 

已经博主授权,源码转载自 https://pan.quark.cn/s/a4b39357ea24 常见问题解答 网页打开速度慢或者打不开网页? 受到多种因素的影响,对于非会员用户我们无法提供最优质的服务。 如果您希望得到最棒的体验,请至大会员页面("右上角菜单 → 大会员")根据说明操作。 请注意:受制于国际网络的诸多不确定性,我们无法对任何服务的可靠性做出任何保证。 如果出现了网络连接相关的问题,我们建议您先等待一段时间,之后再重试。 如果您在重试后发现问题仍然存在,请联系我们,并说明网络问题持续的时间。 图片下载后无法找到? 打开"右上角菜单 → 更多 → 修改下载路径",在弹出的对话框中可以看到当前图片的保存路径。 此外,由于网络因素,在保存图片之后,等待屏幕下方出现"已保存到..."后,才能在本地找到图片。 如何更改图片保存的目录? 请参见"右上角菜单 → 更多 → 修改下载路径"。 翻页不方便? 在点进某个图片后,通过在图片上向左或向右滑动,即可翻页查看下一个作品。 如何保存原图/导出动图? 长按图片/动图,在弹出的菜单中选择保存/导出即可。 输入账号密码后出现"进行人机身份验证"? 此为pixiv登陆时的验证码,请按照要点击方框或图片。 在pxvr中注册pixiv账号后,收到验证邮件,无法访问邮件中的验证链接? 请复制邮件中的链接,打开pxvr中的"右上角菜单 → 输入地址"进行访问。 能否自动将页面内容翻译为汉语? 很抱歉,pxvr暂不提供语言翻译服务。 图片下载类型是否可以选择? 能否批量下载/批量管理下载? 已支持批量下载多图作品中的所有原图:找到一个多图作品,进入详情页面后,点击图片进入多图浏览模式,长按任意一张图片即可看到批量下载选项。 关于上述其他功能,我们...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心脏dance

如果解决了您的疑惑,谢谢打赏呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值