题目链接:

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 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
 分析:
开始是想用备忘录加递归来做的,定义一个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;
}

  

HDU 1165 公式推导题的更多相关文章

  1. HDU-1042-N!(Java大法好 &amp;&amp; HDU大数水题)

    N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Subm ...

  2. HDU 自动刷题机 Auto AC (轻轻松松进入HDU首页)

    前言: 在写这篇文章之前,首先感谢给我思路以及帮助过我的学长们 以下4篇博客都是学长原创,其中有很多有用的,值得学习的东西,希望能够帮到大家! 1.手把手教你用C++ 写ACM自动刷题神器(冲入HDU ...

  3. hdu 5326(基础题) work

    http://acm.hdu.edu.cn/showproblem.php?pid=5326 一道水题,题目大意是在公司里,给出n个员工和目标人数m,然后下面的n-1行是表示员工a管理b,问在这些员工 ...

  4. 【转载】 HDU 动态规划46题【只提供思路与状态转移方程】

    1.Robberies 连接 :http://acm.hdu.edu.cn/showproblem.php?pid=2955      背包;第一次做的时候把概率当做背包(放大100000倍化为整数) ...

  5. hdu 5162(水题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5162 题解:看了半天以为测试用例写错了.这题玩文字游戏.它问的是当前第i名是原数组中的第几个. #i ...

  6. hdu 4908(思路题)

    BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. hdu 1165 Eddy&#39;s research II(数学题,递推)

    // Eddy 继续 Problem Description As is known, Ackermann function plays an important role in the sphere ...

  8. 2015ICPC chanchun HDU 5534 (树形题转换完全背包)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题意:给你n个点,让你加上n-1条边使他变成一棵树,题目首先给你a[1] a[2].....a[n- ...

  9. HDU 5391 水题。

    E - 5 Time Limit:1500MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Statu ...

随机推荐

  1. JDBC入门(3)--- PrepareStatement

    一.PrepareStatement概述 PrepareStatement是Statement接口的子接口: 1.强大之处: 防SQL攻击: 提高代码的可读性: 提高效率; 2.PrepareStat ...

  2. RequireJS 是一个JavaScript模块加载器

    RequireJS 是一个JavaScript模块加载器.它非常适合在浏览器中使用, 它非常适合在浏览器中使用,但它也可以用在其他脚本环境, 就像 Rhino and Node. 使用RequireJ ...

  3. C# 读取config

    控制台应用程序 App.config: AppSettings.config: Program.cs: string a = ConfigurationManager.AppSettings[&quo ...

  4. MvcForum作者称该项目进入缓慢更新

    MvcForum作者在github上发表 This project is no longer actively developed as I don't have the time. As and w ...

  5. Setting up a Single Node Cluster Hadoop on Ubuntu/Debian

    Hadoop: Setting up a Single Node Cluster. Hadoop: Setting up a Single Node Cluster. Purpose Prerequi ...

  6. seacms 6.45 命令执行漏洞分析

    前言 这是一个比较老的漏洞了,不过漏洞原理还是挺有意思的. 正文 漏洞位于 search.php 文件中. 首先包含了 common.php, 这个文件里面做了一些初始化工作,其中最重要的是对提交参数 ...

  7. Redis(二):c#连接Redis

    1.nuget StackExchange.Redis 2.建立RedisHelper类: public class RedisHelper { /// <summary> /// 连接字 ...

  8. SQL server 和 Oracle 中列转行的小操作

    Oracle: create table zjhis.mz_zdxx_zl as select a.sfsb, wm_concat(a.zdmc) as 诊断 from zjhis.mz_zdxx a ...

  9. visual studio 2017安装教程以及各类问题解决方案

    文章的关键词和所含教程: VS2017安装/visual studio 2017安装/Xamarin/Android for visual studio 2017/VS2017找不到网站/VS2017 ...

  10. Mac快捷锁屏设置

    Mac快捷锁屏设置: 1. 安全性与隐私 - 通用:进入睡眠或开始屏幕保护程序 --> “ 立即 ”. 2. 桌面与屏幕保护程序 - 触发角:启动屏幕保护程序 注:第二步中触发角可以设置四个角中 ...