首页 > 其他 > 详细

HDU 1165 Eddy's research II (推公式)

时间:2014-08-07 00:44:57      阅读:430      评论:0      收藏:0      [点我收藏+]

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 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:
bubuko.com,布布扣

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
 
看到这道题的0<m<=3,所以觉得可以分情况讨论推公式,试了几组数据才得出推得公式。
bubuko.com,布布扣
 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

这里我还没有推出m=3的时候的公式,后来比完赛GJ告诉我他推得,瞬间觉得可以更加优化

bubuko.com,布布扣
 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

 

HDU 1165 Eddy's research II (推公式),布布扣,bubuko.com

HDU 1165 Eddy's research II (推公式)

原文:http://www.cnblogs.com/clliff/p/3896084.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!