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. Mysql+php报错原因

    SQL syntax --语法错误,看near,错误会在near后引号中的内容 的附近 Table/Database....... dosen't existes ---表/库(库名/表名) 不存在 ...

  2. day16 web前端之JavaScript

    页面布局补充 样例页面: 示例代码: <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  3. nodeJs学习-05 案例:http/fs/querystring/url

    const http = require('http'); const fs = require('fs'); const querystring = require('querystring'); ...

  4. OpenCV 新手教程 之环境配置 + 图片匹配 matchTemplate

    1.什么是OpenCV OpenCV的全称是:Open Source Computer Vision Library. OpenCV是一个基于(开源)发行的跨平台计算机视觉库,能够执行在Linux.W ...

  5. SDUT-1479_数据结构实验之栈与队列九:行编辑器

    数据结构实验之栈与队列九:行编辑器 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个简单的行编辑程序的功能是:接受用 ...

  6. Java练习 SDUT-1132_斐波那契数列

    C/C++经典程序训练2---斐波那契数列 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 编写计算斐波那契(Fibon ...

  7. 直击 KubeCon 现场 | 阿里云 Hands-on Workshop 亮点回顾

    相关文章链接[合集]规模化落地云原生,阿里云亮相 KubeCon China沉淀九年,一文看清阿里云原生大事件 2019 年 6 月 24 日至 26 日,KubeCon + CloudNativeC ...

  8. 2019-8-31-dotnet-通过-WMI-获取设备厂商

    title author date CreateTime categories dotnet 通过 WMI 获取设备厂商 lindexi 2019-08-31 16:55:59 +0800 2019- ...

  9. @51nod - 1196/1197/1198@ 字符串的数量

    目录 @description@ @solution@ @part - 1@ @part - 2@ @part - 3@ @accepted code@ @details@ @description@ ...

  10. autocomplete="off" inpu属性

    input 的属性autocomplete 默认为on 其含义代表是否让浏览器自动记录之前输入的值 很多时候,需要对客户的资料进行保密,防止浏览器软件或者恶意插件获取到 可以在input中加入auto ...