hdu 1165 Eddy's research II

简介: hdu 1165 Eddy's research II

Eddy's research II

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6240    Accepted Submission(s): 2217


 

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

 

一开始读完题想到的是直接递归,但发现这样肯定会超时,想试试递推,发现有点麻烦,然后打表找规律,发现规律如下:

m=0 时                  A(m,n)=n+1

m>0&&n==0时     A(m,n)=2

m=1&&n>0时       A(m,n)=n+2

m=2&&n>0时       A(m,n)=2*(n+1)+1

m=3&&n>0时       符合以下递推公式

A[0]=13;  A[i]=A[i-1]*2+3;

 

 

 

AC代码如下:

#include<stdio.h>
/*
int A(int m,int n)
{
if(m==0) return n+1;
if(m>0&&n==0) return A(m-1,1);
if(m>0&&n>0) return A(m-1,A(m,n-1));
}
*/
int main()
{
__int64 n,m,i,A[25];
A[0]=13;
for(i=1;i<25;i++) A[i]=A[i-1]*2+3;
while(scanf("%I64d %I64d",&m,&n)!=EOF)
{
if(m==0) printf("%I64d\n",n+1);
if(m>0&&n==0) printf("2\n");
if(m>0&&n>0)
{
if(m==1) printf("%I64d\n",n+2);
if(m==2) printf("%I64d\n",2*(n+1)+1);
if(m==3) printf("%I64d\n",A[n-1]);
}
//    printf("%d\n",A(m,n));
}
return 0;
}
目录
相关文章
UVa11506 - Angry Programmer(ISAP)
UVa11506 - Angry Programmer(ISAP)
65 0
The Preliminary Contest for ICPC China Nanchang National Invitational M题 Subsequence
The Preliminary Contest for ICPC China Nanchang National Invitational M题 Subsequence
81 0
|
机器学习/深度学习 人工智能
The Preliminary Contest for ICPC China Nanchang National Invitational K题 MORE XOR
The Preliminary Contest for ICPC China Nanchang National Invitational K题 MORE XOR
81 0
UVA10474 大理石在哪儿 Where is the Marble?
UVA10474 大理石在哪儿 Where is the Marble?
|
人工智能 BI 算法
|
机器学习/深度学习 自然语言处理
|
人工智能 Java C++