fjnu 1189 Recurrence Relations

来源:互联网 发布:java音乐网站源码 编辑:程序博客网 时间:2024/05/28 22:12

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

1457

 

Sample Output

132035586497

 

 

 

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的,所以采用数组来算,后面的只要出数组拿值;

原创粉丝点击