51Nod 2656 阿克曼函数#
目录#
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));
}
联系邮箱:curren_wong@163.com
Github:https://github.com/CurrenWong
欢迎转载/Star/Fork,有问题欢迎通过邮箱交流。