Fibonacci Again HDU-1021

本文探讨了一道编程题目,通过观察F(n)序列与3的倍数之间的规律,找到了一种高效判断F(n)是否为3的倍数的方法。作者最初尝试直接计算F(n),但发现这种方法在n较大时会超出long long类型范围,最终通过寻找规律简化了解题过程。

原题:https://cn.vjudge.net/problem/HDU-1021
本来是非常简单的,我原来的思路就是把从0到1 000 000F(n)计算出来然后存起来,然后在判断是否可以被3整除。

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>

#define vint vector<int>
#define vstr vector<string>
#define vll vector<long long>
#define ll long long
#define pf printf
#define sf scanf
#define pft	printf("\t")
#define pfn printf("\n")
#define pfk printf(" ")
#define N 1000000007
#define PI 3.1415926

using namespace std;

int a[1000100];

int main() {
	int n;
	a[0] = 7;
	a[1] = 11;
	for( int i=2; i<1000100; i++ ) {
		a[i] = a[i-1]+a[i-2];
	}
	while( cin >> n ) {
//		cout << a[n];
//		pfn;
		if( a[n]%3==0 ) {
			cout << "yes";
			pfn;
		}else {
			cout << "no";
			pfn;
		}
	}
	return 0;
}

最后提交结果是WA,我很纳闷,就问了同学,大佬告诉我n接近1 000 000时long long 都爆了(实际上F(90)应该就会爆),所以这种解法肯定不行。
之后我就找了题解,这道题最简单的就是找规律了;

n01234567891011121314
F(n)711182947761231993225218431364220735715778
结果nonoyesnononoyesnononoyesnononoyes

可以看出n = 2 + 4*k(k是非负整数)时,F(n)%3==0即“yes”
所以AC代码:

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>

#define vint vector<int>
#define vstr vector<string>
#define vll vector<long long>
#define ll long long
#define pf printf
#define sf scanf
#define pft	printf("\t")
#define pfn printf("\n")
#define pfk printf(" ")
#define N 1000000007
#define PI 3.1415926

using namespace std;

int main() {
	int n;
	while( cin >> n ) {
		if( (n-2)%4==0 ) {
			cout << "yes";
		}else {
			cout << "no";
		}
		pfn;
	}
	return 0;
}
### 斐波那契数列的 RISC-V 实现 斐波那契数列是一种经典的递归算法,在 RISC-V 架构下的实现可以通过汇编语言完成。以下是基于 RISC-V 汇编语言的一个简单示例,用于计算第 n 项斐波那契数值。 #### 示例代码 以下是一个简单的 RISC-V 汇编程序,实现了斐波那契数列的递归计算: ```assembly # Fibonacci sequence implementation in RISC-V assembly language .data result_msg: .asciz "Fibonacci result is %d\n" .text .globl fib fib: # Base case: if n == 0 or n == 1, return n li t0, 0 # Load immediate value 0 into temporary register t0 beq a0, t0, base_case_0 # If input (a0) equals 0, jump to base_case_0 li t0, 1 # Load immediate value 1 into temporary register t0 beq a0, t0, base_case_1 # If input (a0) equals 1, jump to base_case_1 # Recursive case: calculate fib(n-1) and fib(n-2) addi sp, sp, -8 # Allocate space on the stack for two values sw ra, 4(sp) # Save return address onto the stack sw s0, 0(sp) # Save s0 (used as a temporary variable) addi a0, a0, -1 # Decrement argument by 1 jal fib # Call fib recursively with decremented argument lw s0, 0(sp) # Restore saved s0 from the stack mv s0, a0 # Move returned value of fib(n-1) into s0 lw ra, 4(sp) # Restore return address addi sp, sp, 8 # Deallocate stack space addi a0, a0, -2 # Decrement original argument by 2 jal fib # Call fib again with further decremented argument add a0, s0, a0 # Add results of fib(n-1) and fib(n-2), store in a0 jr ra # Return to caller base_case_0: li a0, 0 # Set result to 0 jr ra # Return to caller base_case_1: li a0, 1 # Set result to 1 jr ra # Return to caller main: li a0, 10 # Input value (n = 10) jal fib # Calculate fibonacci(10) la a0, result_msg # Load address of format string ecall # Print result using system call ``` 上述代码展示了如何在 RISC-V 中通过递归来实现斐波那契数列的计算[^3]。`fib` 函数接受输入 `a0` 并返回相应的斐波那契值。对于基线情况 (`n = 0` 或 `n = 1`),函数直接返回输入值;而对于其他情况,则调用自身两次分别计算前两项并相加得到最终结果。 #### 关键点解析 1. **栈管理**: 在递归过程中,为了保存中间状态(如寄存器值和返回地址),需要合理利用堆栈空间。这里使用了 `sp` 寄存器作为堆栈指针,并通过 `sw` 和 `lw` 指令存储/恢复数据。 2. **伪指令应用**: 尽管此代码未显式涉及伪指令,但在实际开发中可以考虑引入诸如 `nop`, `mv` 等简化复杂度的操作。 3. **性能优化**: 上述方法虽然直观易懂,但由于重复子问题的存在可能导致效率低下。可通过动态规划或记忆化技术改进其时间复杂度[^1]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值