Eddy's research II (记忆化找规律)

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

兴致满满的打出记忆化,结果发现3 1000的数据就出不来。又看一眼题面,发现m就1 2 3三个取值,所以完全可以找规律,记忆化找规律哈哈哈哈哈,代码如下:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
int n,m;
ll ff(int n)
{
    ll s=1;
    for(int i=0;i<n;i++)
        s*=2;
    return s;
}
int fun()
{
    if(m==0)
        return n+1;
    if(m==1)
        return n+2;
    if(m==2)
        return 2*n+3;
    if(m==3)
        return ff(n+3)-3;
}
int main()
{
//	            freopen("C:\\Users\\nuoyanli\\Desktop\\acminput.txt","r",stdin);
//    freopen("C:\\Users\\nuoyanli\\Desktop\\acmoutput.txt","w",stdout);
    while(cin>>m>>n)
        cout<<fun()<<endl;
    return 0;
}

 

全部评论

相关推荐

05-29 09:02
门头沟学院 Java
点赞 评论 收藏
分享
06-23 11:28
门头沟学院 Java
牛客91966197...:也有可能是点拒绝的时候自动弹的话术
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
今天 14:00
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务