TopCoder

User's AC Ratio

66.7% (4/6)

Submission's AC Ratio

40.0% (4/10)

Tags

Description

temmie 學完遞迴課後,他回想了以前他看過一個遞迴關係式如下:

$$
f(n) = \begin{cases}
1 & \text{if } n=0\\
2 & \text{if } n=1\\
2 \times f(x-1) - f(x-2) + (x \bmod 3) & \text{otherwise}\\
\end{cases}
$$

現在他想使用程式實作出這個記憶中的遞迴關係式,請你幫助他。

// 以下是上傳的內容:
int f(int n, int memorize_data[]){
    // TODO
}

這個題目只要上傳你寫的函數就好。不要上傳整個 .cpp 檔案。

一個不保證會 AC 的範例如下:

// 以下是上傳的內容
int f(int n, int memorize_data[]){
return 1;
}

當你上傳程式碼片段後,它會被放在以下位置:

#include <iostream>
using namespace std;

int f(int n, int memorize_data[]);

int main(){

    int n;
    int memorize_data[10001];

    // init
    for (int i=0 ; i<=10000 ; i++){
        memorize_data[i] = 0;
    }

    cin >> n;
    cout << f(n, memorize_data) << "\n";
    return 0;
}

// 你的程式碼會被放在這裡

Input Format

第一行為一個非負整數 $n$。

測資範圍限制

  • $0 \le n \le 10000$

Output Format

請輸出該函數代入 $n$ 後的值。

可以保證在給定的範圍中,輸出的值會在 $[1, 2147483647]$ 之間。

Sample Input 1

10

Sample Output 1

59

Sample Input 2

10000

Sample Output 2

50008334

Hints

請注意,使用普通遞迴的方式會無法得到完整的分數(在第二個子問題中,會全部得到 TLE),需使用記憶化的方式才能獲得本題的 AC。

Problem Source

Subtasks

No. Testdata Range Constraints Score
1 0, 2~5 $0 \le n \le 20$ 50
2 0~9 50

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 1000 65536 65536 1 2
1 1000 65536 65536 2
2 1000 65536 65536 1 2
3 1000 65536 65536 1 2
4 1000 65536 65536 1 2
5 1000 65536 65536 1 2
6 1000 65536 65536 2
7 1000 65536 65536 2
8 1000 65536 65536 2
9 1000 65536 65536 2