hdu 1165 递推

喜欢ヅ旅行 2022-08-21 04:28 268阅读 0赞

题目:

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:
1165-1.jpg

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

其实就是给出这样一个表达式,给出m、n得到A(m,n)的值。

1165-1.jpg

这个是数据范围:

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.

一道递推问题,具体看代码。

  1. /*
  2. m=0时:
  3. A(0,n) = n+1 ;
  4. m=1时:
  5. A(1,n) = A(1-1,A(1,n-1)) = A(0,A(1,n-1))
  6. = A(1,n-1)+1 = A(0,A(1,n-2) +1 = A(1,n-2)+ 2 = ...
  7. = A(1,n-n)+ n = A(1,0) + n = A(0,1)+n = n+2 ;
  8. m=2时:
  9. A(2,n) = A(1,A(2,n-1)) = A(2,n-1)+2 =
  10. A(1,A(2,n-2))+2 = A(2,n-2)+2*2 = ... =
  11. A(2,n-n)+2*n = A(2,0)+2*n =
  12. A(1,1)+2*n = 2*n +3 ;
  13. m=3时:直接递归求解就可以了
  14. 也可以这样先递推一下再来,A(3,m) = A(2,A(3,n-1)) = 2*A(3,(n-1)) + 3 ;
  15. */
  16. #include <map>
  17. #include <queue>
  18. #include <stack>
  19. #include <cmath>
  20. #include <cstdio>
  21. #include <cstring>
  22. #include <iostream>
  23. #include <algorithm>
  24. using namespace std ;
  25. #define mem(a) memset(a,0,sizeof(a))
  26. #define inf 100000005
  27. int const maxn = 1000005 ;
  28. long long cal(int m,int n)
  29. {
  30. if(m==0)return n+1;
  31. if(m==1)return n+2;
  32. if(m==2)return 2*n+3 ;
  33. if(m>0&&n==0)return cal(m-1,1);
  34. if(m>0&&n>0)return cal(m-1,cal(m,n-1));
  35. }
  36. int main()
  37. {
  38. int m,n;
  39. while(scanf("%d%d",&m,&n)!=EOF)
  40. {
  41. if(m==3)printf("%I64d\n",cal(m,n));
  42. else if(m==1)printf("%d\n",n+2);
  43. else if(m==0)printf("%d\n",n+1);
  44. else if(m==2)printf("%d\n",2*n+3);
  45. }
  46. return 0 ;
  47. }

发表评论

表情:
评论列表 (有 0 条评论,268人围观)

还没有评论,来说两句吧...

相关阅读

    相关 hdu5286 dp+

    题意: 有n个人,每个人有一个value值,有两扇门A,B分别各有一个value,现在要讲n个人分为两组分别从两个门进入,要求是每组人的value值的和(迭代的求,直至变为一

    相关 hdu5375 dp+

    题意: 给出一段二进制码,其中有些位置的数字不确定用"?"表示,这些位置可以为0也可以为1,然后将这个数字的所有可能转化为相应的格雷码,格雷码对应的数字串中位置为i的数字是1

    相关 hdu4489 dp+

    题意: 有n个高矮不同的士兵,现在要将他们按高,矮依次排列,问有多少种情况。 分析: 组合dp问题。 假设n个士兵的身高分别为1,2......n。 我们现在考虑n个

    相关 hdu4045()

    不会斯特林数的只能用递推思想了,结果发现推出来的就是斯特林数。。。 include <stdio.h> include <stdlib.h> incl