Piotr found a magical box in heaven. Its magic power is that if you place any red balloon inside it
then, after one hour, it will multiply to form 3 red and 1 blue colored balloons. Then in the next hour,
each of the red balloons will multiply in the same fashion, but the blue one will multiply to form 4 blue
balloons. This trend will continue indefinitely.
  The arrangements of the balloons after the 0-th, 1-st, 2-nd and 3-rd hour are depicted in the
following diagram.
  As you can see, a red balloon in the cell (i, j) (that is i-th row and j-th column) will multiply to
produce 3 red balloons in the cells (i ∗ 2 − 1, j ∗ 2 − 1), (i ∗ 2 − 1, j ∗ 2), (i ∗ 2, j ∗ 2 − 1) and a blue
balloon in the cell (i ∗ 2, j ∗ 2). Whereas, a blue balloon in the cell (i, j) will multiply to produce 4 blue
balloons in the cells (i ∗ 2 − 1, j ∗ 2 − 1), (i ∗ 2 − 1, j ∗ 2), (i ∗ 2, j ∗ 2 − 1) and (i ∗ 2, j ∗ 2). The grid size
doubles (in both the direction) after every hour in order to accommodate the extra balloons.
  In this problem, Piotr is only interested in the count of the red balloons; more specifically, he would
like to know the total number of red balloons in all the rows from A to B after K-th hour.
Input
  The first line of input is an integer T (T < 1000) that indicates the number of test cases. Each case
contains 3 integers K, A and B. The meanings of these variables are mentioned above. K will be in
the range [0, 30] and 1 ≤ A ≤ B ≤ 2K.
Output
  For each case, output the case number followed by the total number of red balloons in rows [A, B] after
K-th hour.
Sample Input
3
0 1 1
3 1 8
3 3 7
Sample Output
Case 1: 1
Case 2: 27
Case 3: 14

解题思路:

  以f(k,i)表示第k小时时前i行的红色气球数,则有如下递归表达式:

  f(k,i) = 2*f(k-1,i)                ,i<=2^(k-1)

      或= 3^(k-1)+f(k-1,i-2^(k-1))     ,i>2^(k-1)

注意:数据要用long long 类型

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <assert.h>
using namespace std;
typedef long long LL;
LL C(int k){
LL ans=;
for(int i=;i<k;i++)
ans*=;
return ans;
}
LL f(int k, int i){
//assert(<=i&&i<=(<<k));
if(i<=) return ;
if(k==) return ; if(i<=(<<(k-))) return *f(k-,i);
else return *C(k-)+f(k-,i-(<<(k-)));
}
int main(int argc, const char * argv[]) {
int T;
scanf("%d",&T);
int kase =;
while(T--){
printf("Case %d: ",kase++);
int k,A,B;
scanf("%d%d%d",&k,&A,&B);
printf("%lld\n",f(k,B)-f(k,A-));
}
return ;
}

12627 - Erratic Expansion——[递归]的更多相关文章

  1. Uva 12627 Erratic Expansion(递归)

    这道题大体意思是利用一种递归规则生成不同的气球,问在某两行之间有多少个红气球. 我拿到这个题,一开始想的是递归求解,但在如何递归求解的思路上我的方法是错误的.在研读了例题上给出的提示后豁然开朗(顺便吐 ...

  2. uva 12627 - Erratic Expansion(递归求解)

    递归的边界条件写的多了--不是必需写呢么多的.. 不明确可共同探讨~ #include<cstdio> #include<iostream> #include<cmath ...

  3. UVA - 12627 Erratic Expansion(奇怪的气球膨胀)(递归)

    题意:问k小时后,第A~B行一共有多少个红气球. 分析:观察图可发现,k小时后,图中最下面cur行的红气球个数满足下式: (1)当cur <= POW[k - 1]时, dfs(k, cur) ...

  4. UVa 12627 Erratic Expansion - 分治

    因为不好复制题目,就出给出链接吧: Vjudge传送门[here] UVa传送门[here] 请仔细看原题上的那幅图,你会发现,在时间t(t > 0),当前的气球构成的一幅图,它是由三个时间为( ...

  5. UVA 12627 - Erratic Expansion

    一个红球能够分裂为3个红球和一个蓝球. 一个蓝球能够分裂为4个蓝球. 分裂过程下图所看到的: 设当前状态为k1.下一状态为k2. k1的第x行红球个数 * 2 ⇒ k2第2*x行的红球个数. k1的第 ...

  6. UVA - 12627 Erratic Expansion 奇怪的气球膨胀 (分治)

    紫书例题p245 Piotr found a magical box in heaven. Its magic power is that if you place any red balloon i ...

  7. 【数形结合】Erratic Expansion

    [UVa12627]Erratic Expansion 算法入门经典第8章8-12(P245) 题目大意:起初有一个红球,每一次红球会分成三红一蓝,蓝球会分成四蓝(如图顺序),问K时的时候A~B行中有 ...

  8. UVa 12627 (递归 计数 找规律) Erratic Expansion

    直接说几个比较明显的规律吧. k个小时以后,红气球的个数为3k. 单独观察一行: 令f(r, k)为k个小时后第r行红气球的个数. 如果r为奇数,f(r, k) = f((r+1)/2, k-1) * ...

  9. 【uva 12627】Erratic Expansion(算法效率--递推)

    题意:初始1个红气球,每小时后,1个红气球会变成3个红气球和1个蓝气球,而1个蓝气球会变成4个蓝气球.问经过N小时后,第L~R行一共有多少个红气球. 解法:问行数就定义f[i][j]表示 i 小时后前 ...

随机推荐

  1. Android中View的layout mechanism(布局机制)

    layout mechanism Android中View的layout mechanism主要分为两个阶段:measure阶段和layout阶段.layout mechanism按照一定的顺序进行, ...

  2. Directx11教程(8) 一个新的camera类

    原文:Directx11教程(8) 一个新的camera类      本章我们将替换掉CameraClass类,实现一个稍微靠谱点的摄像机类.并通过Q,W,E,A,S,D,Z,X,C等按键实现摄像机的 ...

  3. oracle中时间格式的转换

    1:取得当前日期是本月的第几周  select to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from dual; TO_CHAR(SYSDATE,'YY') se ...

  4. More Effective C++: 01基础议题

    01:仔细区别 pointers 和 references 1:没有所谓的null reference,但是可以将 pointer 设为null.由于 reference 一定得代表某个对象,C++ ...

  5. poj 2385【动态规划】

    poj 2385 Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14007   Accepte ...

  6. HTML5入门指南

    1.HTML5到底是什么? HTML5是HTML最新的修订版本,2014年10月由万维网联盟(W3C)完成标准制定.目标是取代1999年所制定的HTML 4.01和XHTML 1.0标准,以期能在互联 ...

  7. Nuxt.js打造旅游网站第2篇_首页开发

    页面效果: 1.初始化默认布局 nuxtjs提供了一个公共布局组件layouts/default.vue,该布局组件默认作用于所有页面,所以我们可以在这里加上一些公共样式,在下一小结中还会导入公共组件 ...

  8. 【NS2】各种TCP版本 之 TCP Tahoe 和 TCP Reno(转载)

    实验目的 学习TCP的拥塞控制机制,并了解TCP Tahoe 和 TCP Reno的运行方式. 基础知识回顾 TCP/IP (Transmission Control Protocol/Interne ...

  9. ADO.NET_02

    一.说明 这个例子是小白跟着学习代码记录,模拟用户登陆功能,并可以查询所有学生信息. 二.代码 共4个文件,如下 App.config <?xml version="1.0" ...

  10. AUTO uninstaller 密钥/激活码/破解/注册机 ver 8.9.05 资源下载【转载】

    技术贴:AUTO uninstaller 密钥/激活码/破解/注册机 ver 8.9.05 资源下载 楼主分享几个auto uninstaller密钥破解注册机,可以用于AUTO uninstalle ...