HDU 1165 Eddy's Research II (formula)

Source: Internet
Author: User
Eddy's Research II

Time Limit: 4000/2000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3122 accepted submission (s): 1137


Problem descriptionas 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 ).

 

Inputeach 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.

 

Outputfor each value of M, N, print out the value of A (m, n ).

 

Sample input1 32 4

 

Sample output511

 

Authoreddy

 

Recommendjgshining sees 0 <m <= 3 for this question. Therefore, we can discuss the push formula based on different situations and try several groups of data to obtain the push formula.
 1 #include<cstdio> 2 #include<cstring> 3 #include<stdlib.h> 4 #include<algorithm> 5 using namespace std; 6 __int64 A(int m,int n) 7 { 8     if(n==0)   return A(m-1,1); 9     else if(m==0)  return n+1;10     else if(m==1)  return n+2;11     else if(m==2)   return 2*n+3;12     else if(m==3)   return 2*A(3,n-1)+3;13 }14 int main()15 {16     int m,n;17     while(scanf("%d %d",&m,&n)!=EOF)18     {19         printf("%I64d\n",A(m,n));20     }21     return 0;22 }
View code

I have not introduced the formula for M = 3, but later I told him that the formula was more optimized than the final game gj.

 1 #include<cstdio> 2 #include<cstring> 3 #include<stdlib.h> 4 #include<algorithm> 5 using namespace std; 6 __int64 A(int m,int n) 7 { 8      if(m==0)  return n+1; 9      if(m==1)  return n+2;10      if(m==2)  return 2*n+3;11      if(m==3)  return (1<<(n+3))-3;12 }13 int main()14 {15     int m,n;16     while(scanf("%d %d",&m,&n)!=EOF)17     {18         printf("%I64d\n",A(m,n));19     }20     return 0;21 }
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.