题意:问k小时后,第A~B行一共有多少个红气球。

分析:观察图可发现,k小时后,图中最下面cur行的红气球个数满足下式:

(1)当cur <= POW[k - 1]时,

dfs(k, cur) = dfs(k - 1, cur);

(2)当cur > POW[k - 1]时,

dfs(k - 1, cur) = 2 * dfs(k - 1, cur - POW[k - 1]) + tot[k - 1];

其中,POW[k - 1]为2^(k  - 1),tot[k - 1]为k-1小时后图中的红气球总数,为3^(k - 1)。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 10000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
LL tot[35];
LL POW[35];//记录2的次方
void init(){
tot[0] = 1;
POW[0] = 1;
for(int i = 1; i <= 30; ++i){
tot[i] = 3 * tot[i - 1];
POW[i] = 2 * POW[i - 1];
}
}
LL dfs(int k, int cur){
if(cur == 0) return 0;
if(k == 0) return 1;
if(cur <= POW[k - 1]){
return dfs(k - 1, cur);
}
else{
return 2 * dfs(k - 1, cur - POW[k - 1]) + tot[k - 1];
}
}
int main(){
int T;
scanf("%d", &T);
int kase = 0;
init();
while(T--){
int k, a, b;
scanf("%d%d%d", &k, &a, &b);
printf("Case %d: %lld\n", ++kase, dfs(k, POW[k] - a + 1) - dfs(k, POW[k] - b));
}
return 0;
}

  

UVA - 12627 Erratic Expansion(奇怪的气球膨胀)(递归)的更多相关文章

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

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

  2. UVA 12673 Erratic Expansion 奇怪的气球膨胀 (递推)

    不难发现,每过一个小时,除了右下方的气球全都是蓝色以外,其他都和上一个小时的气球是一样的,所以是可以递推的.然后定义一类似个前缀和的东西f(k,i)表示k小时之后上面i行的红气球数.预处理出k小时的红 ...

  3. UVa 12627 Erratic Expansion - 分治

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

  4. Uva 12627 Erratic Expansion(递归)

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

  5. UVA 12627 - Erratic Expansion

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

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

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

  7. UVa 12627 奇怪的气球膨胀(分治)

    https://vjudge.net/problem/UVA-12627 题意:一开始有一个红气球.每小时后,一个红气球会变成3个红气球和1个蓝气球,而1个蓝气球会变成4个蓝气球.如图所示分别是经过0 ...

  8. 12627 - Erratic Expansion——[递归]

    Piotr found a magical box in heaven. Its magic power is that if you place any red balloon inside it ...

  9. 【数形结合】Erratic Expansion

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

随机推荐

  1. JS enter键一键登录

    $("body").keydown(function (event) { ) { //enter键键值为13 $('.finish-btn').click(); // $('.fi ...

  2. RTL级低功耗设计

    重点:门控时钟和操作数隔离 1.并行与流水的选择 并行与流水都是用面积换速度,但是有时可以降低功耗 并行处理常用于数字信号处理部分,采用并行处理,也已降低系统工作频率从而降低功耗 用两个乘法器取代原设 ...

  3. SystemVerilog Assertion 设计、调试、测试总结(1)

    暑期实习两个月的其中一个任务是:如何在设计中加入断言?以及断言的基本语法.三种应用场景下的断言(如FIFO.FSM.AXI4-lite总线).参考书籍:<System Verilog Asser ...

  4. 常见加密解密 -- pycryptodomex库

    安装 windows pip install pycryptodomex ubuntu pip install pycryptodome 加密方式 单向加密:MD5 只能对数据进行加密,而不能解密 对 ...

  5. Tomcat+JSP经典配置实例

    经常看到jsp的初学者问tomcat下如何配置jsp.servlet和bean的问题,于是总结了一下如何tomcat下配置jsp.servlet和ben,希望对那些初学者有所帮助. 一.开发环境配置 ...

  6. python 阶乘函数

    def num(n): if n == 1: return n return n*num(n-1) print(num(10)) 输出 3628800 该函数使用了递归函数的规则.return 后面为 ...

  7. Xilinx COE文件格式小记

    官方的参考文档是:https://www.xilinx.com/support/documentation/sw_manuals/xilinx11/cgn_r_coe_file_syntax.htm ...

  8. ffmpeg 学习: 003-关键函数介绍

    背景 了解一些关键函数对于开发的帮助比较大. avformat_open_input FFMPEG 打开媒体的过程开始于 avformat_open_input,因此该函数的重要性不可忽视. 在该函数 ...

  9. 使用zabbix server监控tomcat实战案例

    使用zabbix server监控tomcat实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 大家都知道,zabbix server效率高是使用C语言编写的,有很多应用程序 ...

  10. windows系统下使用mycat实现mysql数据库的主从复制,从而实现负载均衡

    在之前有记录过在一台系统中安装多台数据库,同时实现主从复制,但是那个主从复制只是一个基于dosc命令的,再实际的开发中我们不会去直接连接数据库,一般情况下我们也是通过间接的采用一些中间件去连接,本来是 ...