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;
}

 

全部评论

相关推荐

希望被捞的猫头鹰很理智:大概率待遇低怕硕士跑路
点赞 评论 收藏
分享
逆流河上万仙退:如果是能有面试的话应该简历没啥问题 争取表现好一点然后到岗时间实习天数往长了说 先看看能不能有offer
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务