Fibonacci POJ - 3070 (矩阵快速幂的模板)

5G无线关键技术 — 新型传输波形技术和先进编码与调制技术 目录 1、OFDM传输波形技术 2、滤波器组多载波 (Filterbank multicarrier:FBMC) 3、先进编码与调制技术 4、空间调制(Spatial Modulation SM) 5、频率正交幅度调制 1、OFDM传输波形技术 OFDM是当前Wi-Fi和LTE标准中的高速无线通信的主要传信模式。 LTECP配置 子载波间隔 CP长度Tcp 有用符号长度Tu CP比例.. 阅读详情

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:

.


题意:就是求 Fibonacci 数列的第n项的数;

思路:矩阵快速幂,最重要的就是构造式子,

如:在斐波那契数列之中

fi[i] = 1*fi[i-1]+1*fi[i-2]

 fi[i-1] = 1*f[i-1] + 0*f[i-2];

即

所以

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define mod 10000
struct node
{
	ll a[2][2];
};
ll n;

node s_cheng(node tt,node kk)
{
	int i,j,k;
	node c;
	c.a[0][0] = c.a[0][1] = c.a[1][0] = c.a[1][1] = 0;
	for(i=0;i<2;i++)
		for(j = 0;j<2;j++)
			for(k = 0;k<2;k++)
				c.a[i][j] = (c.a[i][j] + tt.a[i][k] * kk.a[k][j])%mod; 
	return c;
}

node jz_pow(node tt,ll n)
{
	node res;
	res.a[0][0] = res.a[1][1] = 1;
	res.a[0][1] = res.a[1][0] = 0;
	while(n)
	{
		if(n&1) res = s_cheng(res,tt);
		n>>=1;
		tt = s_cheng(tt,tt);
	}
	return res;
}
int main()
{
	while(~scanf("%lld",&n)&&n!=-1)
	{
		if(n==0)
		{
			printf("0\n");
			continue;
		} 
		node tt,res;
		tt.a[0][0] = tt.a[0][1] = tt.a[1][0] = 1;
		tt.a[1][1] = 0;
		res.a[0][0] = 1;
		res.a[1][0] = 0;
		tt = jz_pow(tt,n-1);
		res = s_cheng(tt,res);
		printf("%lld\n",res.a[0][0]); 
	}
	return 0;
} 


华为OD机试:查找接口成功率最优时间段 (C/C++/Py/Java/JS/GO) 华为OD机试双机位C卷 查找接口成功率最优时间段,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解。 阅读详情

相关推荐

HDLbits找茬实战:5个Verilog仿真Bug修复案例,新手避坑指南

本文通过HDLbits平台上的5个典型Verilog仿真Bug案例,详细解析了多路选择器位宽不匹配、模块实例化端口映射错误等常见问题,提供实用的修复方案和调试技巧,帮助Verilog新手快速掌握代码验证与仿真技能,避免常见陷阱。

weixin_42653691的博客 165

poj 3070 Fibonacci(矩阵加速DP)

Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12459   Accepted: 8853 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn

clover_hxy的博客 831

SAP PS项目结算实操:从CJ20N到CJ88,手把手搞定项目成本结转与凭证生成

本文详细解析SAP PS项目结算全流程,从CJ20N到CJ88的操作要点,涵盖成本归集、结算规则配置、结果分析及凭证生成等关键环节。特别针对FICO模块中的项目凭证流转问题提供实战解决方案,帮助财务人员规避常见配置错误,提升项目结算效率与准确性。

weixin_26833503的博客 824

NYOJ148fibonacci数列(二)

fibonacci数列(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 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 Fibonacc...

weixin_34174105的博客 281

一文说清Arduino如何实现舵机平滑转动(机器人场景)

深入讲解如何利用arduino控制舵机转动,实现机器人场景中的平稳运动。通过代码优化与脉冲宽度调节,让舵机动作更精准流畅,适用于各类自动化项目。

weixin_35706255的博客 905

在 NVIDIA Orin (JetPack 6.0) 上配置 PyTorch 2.4 + Torchvision 0.19 的完整避坑指南

本文提供在NVIDIA Jetson Orin设备上,基于JetPack 6.0系统配置PyTorch 2.4与Torchvision 0.19环境的完整指南。通过使用Miniconda环境隔离、安装特定版本PyTorch夜间构建版、编译匹配的Torchvision源码等步骤,解决版本不匹配、CUDA支持等常见问题,确保AI开发环境稳定可用。

weixin_29179311的博客 369

POJ 3070 Fibonacci【矩阵连乘】

Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6881   Accepted: 4873 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn

free斩 1628

机器人中的数值优化(二十一)—— 伴随灵敏度分析、线性方程组求解器的分类和特点、优化软件

本系列文章主要是我在学习《数值优化》过程中的一些笔记和相关思考,主要的学习资料是深蓝学院的课程《机器人中的数值优化》和高立编著的《数值最优化方法》等,本系列文章篇数较多,不定期更新,上半部分介绍无约束优化,下半部分介绍带约束的优化,中间会穿插一些路径规划方面的应用实例

慕羽★的博客 4209

POJ - 3070 - Fibonacci (矩阵快速幂 + 斐波那契数列)

Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10096   Accepted: 7208 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn

Never say never 4196

YOLOv11改进 | Mamba-YOLOv11-L:基于SSM的高效全局建模,精度与效率的双重突破

摘要: 本文提出Mamba-YOLOv11-L,通过集成状态空间模型(SSM)改进YOLOv11的全局建模能力。针对YOLOv11在复杂场景中全局依赖缺失、精度-效率矛盾等问题,SSM以$O(N)$计算复杂度实现长程语义捕捉。改进后的模型在工业缺陷检测(Recall↑12%)、边缘设备部署(内存↓16%)、多尺度目标检测(小目标mAP↑9%)等场景表现优异,推理速度保持>40 FPS。核心创新包括轻量化SSM模块设计(稀疏状态转移+局部上下文融合)及与YOLOv11骨干的深度集成,代码实现展示了SSM

走向CTO的路上... 576

031、速度前馈与加速度前馈

摘要: 本文通过追剪机调试案例,阐述了速度前馈和加速度前馈在运动控制中的关键作用。前馈不是高级技巧,而是解决动态响应问题的必要手段:速度前馈通过预测速度变化提前补偿,加速度前馈则针对惯量变化提供力矩补偿。实现时需注意信号平滑处理,避免噪声放大。前馈与PID分工明确,前馈承担主要动态响应,PID处理残余误差。调试应先调前馈再微调PID,前馈增益需计算而非仅凭参数调整,并允许适当残余误差。前馈作为开环补偿不会影响系统稳定性,但需配合平滑的指令生成器使用。(149字)

m0_50546716的博客 303

Fibonacci( POJ-3070)

Fibonacci( POJ-3070) In the Fibonacci integer sequence,F0= 0,F1= 1, andFn=Fn− 1+Fn− 2forn≥ 2. For example, the first ten terms of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 2...

菜鸡的博客 902

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

poj 3070 Fibonacci(矩阵快速幂模板,斐波那契)

Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12812   Accepted: 9109 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = 

TUT我好菜啊 1958

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

POJ 3070 Fibonacci 矩阵快速幂模板

题目链接 题目大意: 求第n项的斐波那契数对10000求余的结果 解题思路: 矩阵快速幂模板题 矩阵快速幂模板: //用二维vector来表示矩阵 typedef vector<int> vec; typedef vector<vec> mat; typedef long long ll; const int M = 10000; //计算矩阵相乘 mat mul(mat ...

RPG_Zero的博客 236

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

POJ 3070 Fibonacci [矩阵快速幂模板]

In the Fibonacci integer sequence,F0= 0,F1= 1, andFn=Fn− 1+Fn− 2forn≥ 2. For example, the first ten terms of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … An alternati...

Vace___yun的博客 208

POJ 3070 Fibonacci【矩阵快速幂取模】【模板题】

递推的式子一般都可以化成矩阵相乘的形式。关键的就是构造矩阵, 但这题题目已经构造好了,所以就成了模(shui)板(shu)题了。

AC_Dreameng 1291

poj 3070 <矩阵快速幂【模板】求Fibonacci数列>

Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12950   Accepted: 9209 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn =

leibniz_zhang的博客 518

poj 3070 Fibonacci (矩阵快速幂乘/模板)

题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long ll; const in...

113

poj3070 Fibonacci(矩阵快速幂模板)

#include<iostream> using namespace std; typedef long long LL; struct mat{ int m[2][2]; }; mat mul(mat a,mat b){ mat ans; for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ ans.m[i][j]=0...

Ellery的book 180

poj 3070 Fibonacci 矩阵快速幂 模板题

矩阵快速幂应该主要在于怎么构造矩阵吧,其他都是模板的。#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define mod 10000 typedef struct { int m[2][2]; }mat; int n; mat mul(ma

liujc_的专栏 400

POJ 3070 Fibonacci(矩阵快速幂)(模板)

传送门 题意:求斐波那契数列的第n项,但是n很大,达到1e9 题解:由于n很大,所以不能逐项递推,采用矩阵快速幂加速递推,如图 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int MOD=1e4; int n; struct Matrix { int v[2][2]; Matrix ()

KGV093的博客 279

POJ 3070 Fibonacci 【矩阵快速幂取模 (模板)】

Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13262   Accepted: 9430 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = 

DTL66的博客 450

POJ3070:Fibonacci(矩阵快速幂模板题)

http://poj.org/problem?id=3070登录后复制 #include <iostream> #include <string.h> #include <stdlib.h> #include <cstdio> #include <algorith...

weixin_33724264的博客 183

hdu 3070 Fibonacci && poj 1575 Tr A(矩阵快速幂模板题)

Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13637   Accepted: 9669 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn =

K键盘里的青春K 736
上一篇: Nested Dolls HDU - 1677 (矩形嵌套问题,先构造序列,再用upper_bound找最长上升子序列+贪心)
下一篇: Seating Arrangement CSU - 1997 (思维题,是否存在n个数的序列,使得相邻啷个数的差绝对值大于d)
obsorb_knowledge
博客等级 码龄9年 38粉丝 213原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值