题目:
Eddy's research II
**Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4689 Accepted Submission(s): 1711
**
\
Problem Description
As is known, Ackermann function plays an important role in the sphere of theoretical computer science. However, in the other hand, the dramatic fast increasing pace of the function caused the value of Ackermann function hard to calcuate.
Ackermann function can be defined recursively as follows:
Now Eddy Gives you two numbers: m and n, your task is to compute the value of A(m,n) .This is so easy problem,If you slove this problem,you will receive a prize(Eddy will invite you to hdu restaurant to have supper).\
\
Input
Each line of the input will have two integers, namely m, n, where 0 < m < =3.
Note that when m<3, n can be any integer less than 1000000, while m=3, the value of n is restricted within 24.
Input is terminated by end of file.\
\
Output
For each value of m,n, print out the value of A(m,n).\
\
Sample Input
1 3
2 4
\
Sample Output
5
11
\
Author
eddy
\
Recommend
JGShining | We have carefully selected several similar problems for you: 1159 1203 1074 1208 1355
\
Statistic | Submit | Discuss | Note
思路:
就是根据上面的图来进行推演,主要是找规律,用深搜和dp都能做,我做了幅图,帮助理解
\
竖着看代表m,横着代表n
题目给的m的范围是从1到3,所以我们观察m等于0时的规律,当m为1时,结果就是当前的n值加上2,当m的值为2的时候,根据上图的式子得到A(1,n-1),所以可以推出公式,s=2*n+3,当m等于3的时候A(2,n-1),利用上面的推导就好,这题限制2秒,所以可以暴力过
代码1(深搜DFS):
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int dfs(int m,int n)
{
if(n==0)return dfs(m-1,1);
if(m==1)return n+2;
if(m==2)return 2*n+3;
if(m==3)return 2*dfs(3,n-1)+3;
}
int main()
{
int m,n;
while(scanf("%d%d",&m,&n)!=EOF)
{
if(m==1)
printf("%d\n",n+2);
else if(m==2)
printf("%d\n",2*n+3);
else
printf("%d\n",dfs(3,n));
}
return 0;
}
代码2(DP):
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int main()
{
int m,n;
while(scanf("%d%d",&m,&n)!=EOF)
{
if(m==1)printf("%d\n",n+2);
else if(m==2)printf("%d\n",2*n+3);
else
{
int sum=5;//A(3,0)=5
for(int i=1; i<=n; i++)
sum=sum*2+3;
printf("%d\n",sum);
}
}
return 0;
}
主要是找规律。。
\