2049: Ackermann's Function
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
![]() | 3s | 8192K | 928 | 273 | Standard |
In this easy problem, you are to write a program to calculate the Ackermann's Function A:
A(0, n) = n+1
A(m, 0) = A(m-1, 1)
A(m, n) = A(m-1, A(m, n-1))
Note that the Ackermann's Function is recursive but not primitive recursive which means that it grows very fast.
Input
The input contains several lines, each of which consists of two integers m and n, 0<=m, n<=4. The last line consists of two zeros, which you should not proceed.
Output
For each pair of m and n, print the value of A(m, n) in a single line.
Sample Input
2 3 3 3 0 0
Sample Output
9 61
This problem is used for contest: 10
#include<iostream>
#define N 100000
using namespace std;
int a[5][N]={0};
int main()
{
int m,n;
for(int i=0;i<=4;i++)
for(int j=0;j<=N;j++)
{
if(i==0) a[0][j]=j+1;
else
if(j==0) a[i][0]=a[i-1][1];
else
a[i][j]=a[i-1][a[i][j-1]];
}
while(scanf("%d%d",&m,&n),m||n)
{
cout<<a[m][n]<<endl;
}
return 0;
}
/*A(0, n) = n+1
A(m, 0) = A(m-1, 1)
A(m, n) = A(m-1, A(m, n-1))*/
这道题用递归是写不了的 只能用循环了