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;
}
// 你的程式碼會被放在這裡
第一行為一個非負整數 $n$。
測資範圍限制
請輸出該函數代入 $n$ 後的值。
可以保證在給定的範圍中,輸出的值會在 $[1, 2147483647]$ 之間。
請注意,使用普通遞迴的方式會無法得到完整的分數(在第二個子問題中,會全部得到 TLE),需使用記憶化的方式才能獲得本題的 AC。
No. | Testdata Range | Constraints | Score |
---|---|---|---|
1 | 0, 2~5 | $0 \le n \le 20$ | 50 |
2 | 0~9 | 50 |