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. AC自动机fail树小结

    建议大家学过AC自动机之后再来看这篇小结 fail树就是讲fail指针看做一条边连成的树形结构 fail指针在AC自动机中的含义是指以x为结尾的后缀在其他模式串中所能匹配的最长前缀的长度 所以在模式串 ...

  2. 阿里云 EMAS HTTPDNS 联合函数计算重磅推出 SDNS 服务,三大能力获得突破

    1. 什么是 HTTPDNS ? 传统的 DNS(Domain Name System)使开发者常面临着域名劫持.调度不精准的问题. HTTPDNS 使用 HTTP 协议替换常用的 UDP 协议,完成 ...

  3. CMake学习笔记五-依赖库添加

    # # 项目名称 # SET(WIS_PROJECT_NAME EXAMPLE) # dependencies SET(DEPENDENCIES #依赖第三方库 ) #Qt模块 SET(QT_MODU ...

  4. github中markdown语言的使用规则

    开始使用github就接触了markdown,确实如它的宗旨所言"易读易写",语法简洁明了,功能比纯文本更强,是一种非常适用于网络的书写语言.并且一大优点是兼容HTML,只要不在m ...

  5. CC-Debugger 最小调试接法

    CC-Debugger 最小调试接法 以 CC2541 为例,最少需要四根 DD DC RST GND. 一般 VCC 目标调试板都有,所以这里你需要将 CC-Debugger 的 PIN 2 和 P ...

  6. List容器-ArrayList

    特点:   有序重复,包括null,通过整数索引访问 实现类ArrayList和LinkedList ArrayList--动态数组   不线程同步  单线程合适 List<String> ...

  7. 小爬爬2:fiddler安装和了解fiddler

    1.解压安装就可以了 fiddler优点:抓取移动和PC机器的请求 2.首先进行证书的配置 如果不配置只能抓取http的请求,https不能抓取. 先选择,第一个"清空所有内容" ...

  8. AT3728 Squirrel Migration

    AT3728 Squirrel Migration 就是给每个点分配两个匹配点(自环除外) 考虑最大值 考虑极限情况:每个边的贡献是min(sz[u],sz[v])*2 证明存在方案: 发现,如果哪边 ...

  9. 设置PHP最长运行时间

    通常来说,默认的PHP程序最大运行时间是30s,如果你的程序运行超过这个时间限制,那么会有类似Maximum execution time of 30 seconds exceeded的报错. 有几种 ...

  10. css浮动规则

    1.浮动元素会从文档正常流中删除,但它仍会影响布局 2.浮动非替换元素必须为其指定width,否则元素的width会趋于0而导致浮动元素不能完整显示3.一旦元素具有了浮动属性,它便成为了一个块级元素. ...