fjnu 1189 Recurrence Relations

本文介绍了一种通过数组而非递归方式计算两个特定递推关系中F(N)的方法,适用于N值范围为1到57的情况。给出了完整的C++实现代码,并提供了计算示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Recurrence relations are where a function is defined in terms of itself and maybe other functions as well. in this problem, we have two functions of interest:

F (N) = F (N - 1) + G (N - 1) ; F (1) = 1

G (N) = G (N - 1) + G (N - 3) ; G (1) = 1, G (2) = 0, G (3) = 1

For a given value of N, compute F (N).

Input

Each line of input will have exactly one integer, 57 > = N > 0.

Output

For each line of input, output F(N).

Sample Input

1
4
57

 

Sample Output

1
3
2035586497

 

 

 

Source:

#include
<iostream>
using namespace std;

unsigned F[
60]={0,1};
unsigned G[
60]={0,1,0,1};

void list()
{
    
int i;
    
for(i=4;i<=57;i++)
        G[i]
=G[i-1]+G[i-3];
    
for(i=2;i<=57;i++)
        F[i]
=F[i-1]+G[i-1];
}


int main()
{
    list();
    
int n;
    
while(cin>>n)
    
{
        cout
<<F[n]<<endl;
    }

    
return 0;
}

KEY: 不能使用递归的方法,会TLE的,所以采用数组来算,后面的只要出数组拿值;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值