首页 技术 正文
技术 2022年11月10日
0 收藏 774 点赞 2,614 浏览 1866 个字

题目链接:

https://www.shuzhiduo.com/A/nAJvRgVxJr/acm.hdu.edu.cn/showproblem.php?pid=1165

Eddy’s research II

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

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:
HDU 1165 公式推导题

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 分析:开始是想用备忘录加递归来做的,定义一个f[][]的二维数组,初始化全部为-1,每次计算之前看看f[m][n]的值,如果不是-1,则表示A(m,n)还没有计算过,递归计算,然后将得到的值存起来(备忘录法),如果值不等于-1,说明A(m,n)计算过了,直接拿来用即可,这样可以解决大量重复的子问题但是,这样做会超时。。。。。。所以第二种做法,公式推导因为m只有4个值,那么我们想想是不是对每个不同的m,都有一个公式与之对应呢?开始推导当m=0的时候, 代入题目给的递归公式可知A(0,n)=n+1当m=1的时候, A(1,n)=A(0,A(1,n-1)) =A(1,n-1)+1………(n个1累加)直到n=1的时候,A(0,1)=2,所以A(1,n)=n+2当m=2的时候, A(2,n)=A(1,A(2,n-1))=A(2,n-1)+2………(n个2的累加)直到n=1的时候,A(1,1)=3,所以A(2,n)=2*n+3当m=3的时候, A(3,n)=A(2,A(3,n-1))=2*A(3,n-1)+3……..(2的n次方个5相乘,2的n次方减一个3相乘,二者相加)直到n=1的时候,A(2,1)=5,所以A(3,n)=2的n+3次方-3 我觉得我可能没有说清楚,但是我自己懂了,我真的很努力去说清楚了,唉,自己表达能力有限,大家自己推导一下肯定是可以推导出来的(前前后后推导了一个多小时)代码如下:

#include<bits/stdc++.h>
using namespace std;
int f(int n)
{
int s=1;
for(int i=1;i<=n;i++)
{
s=s*2;
}
return s;
}
int solved(int m,int n)
{
if(m==0)
{
return n+1;
}else if(m==1)
{
return n+2;
}else if(m==2)
{
return 2*n+3;
}else if(m==3)
{
return f(n+3)-3;
}
}
int main()
{
int n,m;
while(~scanf("%d %d",&m,&n))
{
printf("%d\n",solved(m,n));
}
return 0;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918