文章目錄
1. 題目描述
1.1. Limit
Time Limit: 1000 ms
Memory Limit: 131,072 kB
1.2. Problem Description
阿克曼(Arkmann)函數 中,m與n的定義域是非負整數且本題中 ,。
函數的定義爲:
1.3. Input
兩個整數
1.4. Output
一個整數,的結果
1.5. Sample Input
1 1
1.6. Sample Output
3
1.7. Source
2. 解讀
如果直接使用 進行計算,最後一個點會超時。這時考慮到題目所給的數據範圍 , 比較小,帶入一些數字嘗試找出 的規律。
1 | 1 | 3 | 2 | 1 | 5 |
1 | 2 | 4 | 2 | 2 | 7 |
1 | 3 | 5 | 2 | 3 | 9 |
1 | 2 |
3 | 1 | 13 |
3 | 2 | 29 |
3 | 3 | 61 |
3 | 4 | 125 |
3 | n |
再將找出的規律寫成一個函數進行計算即可。
3. 代碼
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
int akm(int m, int n)
{
if (m == 0) {
return n + 1;
} else if (m > 0 && n == 0) {
return akm(m - 1, 1);
} else if (m > 0 && n > 0) {
return akm(m - 1, akm(m, n - 1));
} else {
return 0;
}
}
long long calculate(int m, int n)
{
if (m == 0) {
return n + 1;
} else if (m == 1) {
return n + 2;
} else if (m == 2) {
return 2 * n + 3;
} else if (m == 3) {
return pow(2, n + 3) - 3;
} else {
return 0;
}
}
int main()
{
// test case
int m, n;
scanf("%d %d", &m, &n);
// 計算
printf("%lld", calculate(m, n));
}
聯繫郵箱:[email protected]
Github:https://github.com/CurrenWong
歡迎轉載/Star/Fork,有問題歡迎通過郵箱交流。