poj 3070 Fibonacci

【算法学习】POJ3070——利用分治法来计算Fibonacci数列的值 DescriptionIn 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 al... 阅读详情

Fibonacci
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9769 Accepted: 6959

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:

.



解题思路:单纯的快速矩阵幂

快速矩阵幂模板:

Matrix add(Matrix a,Matrix b){
	Matrix ans;
	for (int i=0;i<2;i++){
		for (int j=0;j<2;j++){
			ans.mat[i][j]=a.mat[i][j]+b.mat[i][j];
			if (ans.mat[i][j]>=m){
				ans.mat[i][j]%=m;
			}
		}
	}
	return ans;
}
Matrix mul(Matrix a,Matrix b){
	Matrix ans;
	for (int i=0;i<2;i++){
		for (int j=0;j<2;j++){
			ans.mat[i][j]=0;
			for (int k=0;k<2;k++){
				ans.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
				if (ans.mat[i][j]>=m){
					ans.mat[i][j]%=m;
				}
			}
		}
	}
	return ans;
}
Matrix Init(){
	Matrix ans;
	for (int i=0;i<2;i++){
		for (int j=0;j<2;j++){
			if (i==j)
				ans.mat[i][j]=1;
			else
				ans.mat[i][j]=0;
		}
	}
	return ans;
}
Matrix exp(Matrix a,int k){
	Matrix ans=Init();
	while (k){
		if (k&1)
			ans=mul(ans,a);
		a=mul(a,a);
		k>>=1;
	}
	return ans;
}


参考代码:

#include <iostream>
using namespace std;
int m=10000;
struct Matrix{
	long long mat[2][2];
};
Matrix add(Matrix a,Matrix b){
	Matrix ans;
	for (int i=0;i<2;i++){
		for (int j=0;j<2;j++){
			ans.mat[i][j]=a.mat[i][j]+b.mat[i][j];
			if (ans.mat[i][j]>=m){
				ans.mat[i][j]%=m;
			}
		}
	}
	return ans;
}
Matrix mul(Matrix a,Matrix b){
	Matrix ans;
	for (int i=0;i<2;i++){
		for (int j=0;j<2;j++){
			ans.mat[i][j]=0;
			for (int k=0;k<2;k++){
				ans.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
				if (ans.mat[i][j]>=m){
					ans.mat[i][j]%=m;
				}
			}
		}
	}
	return ans;
}
Matrix Init(){
	Matrix ans;
	for (int i=0;i<2;i++){
		for (int j=0;j<2;j++){
			if (i==j)
				ans.mat[i][j]=1;
			else
				ans.mat[i][j]=0;
		}
	}
	return ans;
}
Matrix exp(Matrix a,int k){
	Matrix ans=Init();
	while (k){
		if (k&1)
			ans=mul(ans,a);
		a=mul(a,a);
		k>>=1;
	}
	return ans;
}
int main(){
	Matrix a;
	a.mat[0][0]=a.mat[0][1]=a.mat[1][0]=1;
	a.mat[1][1]=0;
	int n;
	while (cin>>n&&n!=-1){
		Matrix ans=exp(a,n);
		cout<<ans.mat[0][1]%m<<endl;
	}
	return 0;
}





UNet改进(24):注意力机制-从基础原理到高级融合策略 本文系统探讨了如何通过引入各类注意力机制增强UNet在医学图像分割中的性能。文章分为基础篇、进阶篇和高级篇三个层级:基础篇介绍了时序注意力、可变形注意力、分层注意力等8种基础机制;进阶篇提出了5种混合注意力设计;高级篇则探讨了全局+局部+多尺度等复杂组合机制。实验表明,这些注意力模块在心脏MRI、肺部感染等任务中能提升2-15%的分割精度。文章还提供了硬件适配建议和完整实现代码,为研究者提供了全面的技术参考。未来方向包括NAS优化、量子注意力等前沿探索,为UNet在医疗影像等领域的应用革新提供了理论指导。 阅读详情

相关推荐

整车CAN网络错误帧排查方法

本文围绕 CAN 总线错误帧展开,先剖析其产生原因,涵盖物理连接与硬件问题、电磁兼容性问题、总线负载与设计问题及配置问题;再阐述位错误、填充错误等 5 类错误帧类型;接着说明错误帧与总线 BUSOFF 的关系,解析主动 / 被动错误状态及 BUSOFF 的触发与恢复;还结合整车故障案例做实际分析,指出错误帧根源多为线束或控制器问题。最后总结,强调错误帧是 CAN 协议保障高鲁棒性的机制,掌握相关知识助工程师提升 CAN 网络处理能力。

车载网络小D的博客 1246

poj3070Fibonacci

poj3070Fibonacci:http://poj.org/problem?id=3070 Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11699   Accepted: 8306 Description In the

hahaAll的博客 473

ATE测试基础——直流参数测试

芯片测试在发展过程中诞生了许多术语,初次接触的人大部分都会被绕晕。因此为方便广大读者理解,在开始介绍测试项之前,需要对测试领域的名词进行解释。还有部分较为抽象的内容,需要略作展开。DUT——Device Under Test,正在被测试的器件Pin——器件引脚,信号引脚分为输入,输出,三态和双向四类,负责与外界进行信息交互;电源引脚分为电源和地两类,结构与信号引脚不同,负责给器件内部运行和外部输出提供电平。输入:在外部信号和器件内部逻辑之间起缓冲作用的信号输入通道;

你骄傲地飞远的博客 1万+

POJ 3070 Fibonacci

POJ 3070 Fibonacci POJ入口:http://poj.org/problem?id=3070 使用模版【模版】矩阵快速幂 AC代码 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; #d...

恒治学 145

数据分析项目:直播电商用户流失分析

其中,'Tenure'(在网时长)可能是影响用户流失的关键因素,意味着用户在平台上的停留时间越长,流失的可能性可能越低,这提示平台应注重提高用户粘性,增加用户在平台上的活跃度和停留时间。)的分析,挖掘用户流失的原因和模式,分析发现用户投诉处理效率(Complain)、距上次消费时长(DaySinceLastOrder)、会员生命周期(Tenure)等8项核心指标对流失预测贡献度达87%,模型准确率94.58%,并提出相应的建议。重视用户的满意度,及时处理用户的投诉和反馈,不断改进平台的服务和功能。

Nervousr的博客 2459

如何使用3D高斯分布进行环境建模

使用3D高斯分布来进行建模,关键在于通过引入不确定性描述来提高建图的鲁棒性和精度。与传统点云表示相比,3D高斯分布提供了一种更为灵活和有效的方式来处理空间数据,能够通过协方差矩阵表示点的空间不确定性,帮助优化SLAM过程中的定位和地图更新。

JaydenQ的博客 2355

poj 3070 Fibonacci(简单矩阵连乘)

题目:http://poj.org/problem?id=3070 Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10994   Accepted: 7823 Description In the Fibonacci inte

theArcticOcean 663

5分钟搞懂异构图注意力网络(HAN):从元路径到节点聚合的全流程解析

本文深入解析异构图注意力网络(HAN)的核心原理与实战应用,从元路径设计到多级注意力实现的全流程。HAN通过节点级和语义级双重注意力机制,有效处理包含多种节点和边类型的异构图数据,在电商推荐、学术网络分析等场景展现卓越性能。文章提供DGL实现代码和调参技巧,帮助开发者快速掌握这一强大工具。

weixin_29056701的博客 311

POJ 3070 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 alterna

光明~~~ 2万+

网规第三版:第4章网络资源设备(完结)

第 4 章 网络资源设备 调整内容: 更新主流网络设备(交换机、路由器、防火墙)的技术参数与选型标准 增加国产化网络设备与信创产品的相关内容 精简已淘汰的设备类型与技术指标

鹿鸣天涯 267

基于Python的京东商品价格监控与降价提醒系统设计与实现

本文详细介绍了如何使用Python构建一个完整的京东商品价格监控与降价提醒系统。系统通过爬虫技术实时抓取京东商品价格信息,结合数据存储与分析,实现价格波动监控和智能降价提醒功能。文章涵盖了系统设计、技术选型、代码实现以及部署方案,为电子商务价格监控领域提供了一个实用的解决方案。关键词:Python爬虫、京东API、价格监控、降价提醒、自动化。

2201_76125261的博客 404

Fibonacci POJ 3070

Fibonacci POJ 3070 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10096 Accepted: 7208 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n...

king9666的博客 314

智能车图像处理2-入门级分析1 ---基础扫线

PS :入门级文章适合新手,如果已经有基础可以去看进阶篇文章。

taiyuezyh的博客 7742

[POJ3070]Fibonacci

传送门POJ3070 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...

Beginner_df016 351

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

POJ3070Fibonacci 矩阵快速幂

POJ3070原题 [url]http://poj.org/problem?id=3070[/url] 如图,Fibonacci矩阵解法: [img]http://dl.iteye.com/upload/attachment/0072/1923/62d5e015-eb0c-3aac-b108-8bedb1676fbe.png[/img] [code="java"] imp...

从此爱上猫 207

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

【poj3070】 Fibonacci

http://poj.org/problem?id=3070 (题目链接)题意:用矩阵乘法求fibonacci数列的第n项。Solution   矩乘入门题啊,题目把题解已经说的很清楚里= =。   矩乘其实很简单,通过自己YY或者是搜索对于一个递推公式求出它所对应的矩阵,然后套个快速幂就可以迅速求解第n项。代码:// poj3070 #include<algorithm> #include<i

MashiroSky的博客 524

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

poj 3070 Fibonacci (快速矩阵幂)

#include &lt;cstdio&gt; #include &lt;cstdlib&gt; #include &lt;cmath&gt; #include &lt;iostream&gt; #include &lt;cstring&gt; using namespace std; const int n = 2; const int mod = 10000; int a[3][3], ...

GaoJieVery6 222

矩阵十题【六】 poj3070 Fibonacci

矩阵

100days 899

【POJ3070】Fibonacci

题意 求Fibn mod m的值 m=10000,0<=0<=2*109 分析 矩阵快速幂模板 设f(n)={Fibn,Fibn+1},可得到单位矩阵为{(0,1)(1,1) } 代码 #include<iostream> #include<cstdio> #include<cstring> #include<...

Ashley_2013的博客 189

Poj 3070 Fibonacci

Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26063 Accepted: 17394 文章目录Description题意:题解:代码: 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 t

qq_35975367的博客 410

[POJ] 3070 Fibonacci

http://poj.org/problem?id=3070 求fib第n项 裸矩阵快速幂,练手 #include<iostream> #include<cstring> #include<cstdio> using namespace std; const int MOD=10000; inline int rd()...

weixin_34127717的博客 97

poj_3070 Fibonacci

题目链接:http://poj.org/problem?id=3070   //C++代码 #include using namespace std; const int Mod=10000; struct Matrix{ int a[2][2]; }origin,res; Matrix multiply(Matrix x,Matrix y){ Matrix temp; temp.

永恒国度 616

poj3070 Fibonacci 数论专题

数论,对于我只一个初中生来说,确实比较需要智商。。。最近老师布置数论题,我就在努力奋斗(虽然决定要马上写替罪羊树),替罪羊树是什么?我也不知道,只知道是一个平衡树,怎么写?看这篇博客吧(写这道poj3070时还木有写:) 大概就这样吧,然后就木有然后了,怪我喽!!! 题目是这样子的:题意很明确,求第m个斐波那契数MOD10000的结果 easy到了极点,我居然都没有写矩阵快速幂。。。直接水过(

跳动的量子 1056

POJ 3070 Fibonacci(矩阵快速幂)

题目地址:http://poj.org/problem?id=3070 思路:矩阵快速幂和快速幂的思想差不多,快速幂详解地址:http://blog.youkuaiyun.com/qq_25605637/article/details/47357119,按照下面2张图来就行 AC代码: #include #include #include #include #include #incl

Java识堂,一个有干货的微信公众号 472
上一篇: poj 3233 Matrix Power Series
下一篇: poj 1656 Counting Black
JinbaoSite0144
博客等级 码龄12年 231粉丝 157原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值