---
# System prepended metadata

title: 13690 - Bad Fibonacci's soup
tags: [recursion]

---

# 13690 - Bad Fibonacci's soup
>author: Utin
###### tags: `recursion`

---

## Brief
See the code below

## Solution 0

```c=
#include <stdio.h>

int a, b, c, d, n;

//座標代表要被換的值，第0位存要換成的值，第1位存有無紀錄過
long long int farr[100][2], garr[100][2];

long long int f(int i);
long long int g(int i);

int main() {
    scanf("%d %d %d %d %d", &a, &b, &c, &d, &n);
    printf("%lld %lld\n", f(n), g(n));
}

long long int f(int i) {
    if(i == 0) return a;
    else if(i == 1) return b;

    //考慮是否被換過
    else if(farr[i][1] == 1) return farr[i][0];
    else if(i%3 == 0) {
        farr[i][0] = f(i-1) + g(f(i/3)%i); //mod是取餘數
        farr[i][1] = 1;
        return farr[i][0];
    }
    else {
        farr[i][0] = f(i-1) + g(i-2);
        farr[i][1] = 1;
        return farr[i][0];
    }
}

long long int g(int i) {
    if(i == 0) return c;
    else if(i == 1) return d;

    //考慮是否被換過
    else if(garr[i][1] == 1) return garr[i][0];
    else if(i%5 == 0) {
        garr[i][0] = g(i-1) + f(g(i/5)%i); //mod是取餘數
        garr[i][1] = 1;
        return garr[i][0];
    }
    else {
        garr[i][0] = g(i-1) + f(i-2);
        garr[i][1] = 1;
        return garr[i][0];
    }
}
// By Utin
```

## Reference