题意:问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. 第2节 storm实时看板案例:10、redis的安装使用回顾

    2.redis的持久化机制: redis支持两种持久化机制:RDB  AOF RDB:多少秒之内,有多少个key放生变化,将redis当中的数据dump到磁盘保存,保存成一个文件,下次再恢复的时候,首 ...

  2. Jquery动画效果设置大全

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 攻防世界web新手练习区(2)

    弱认证:http://111.198.29.45:43769/ 打开是这个页面: 用户名输入1,密码输入2,试试看.会提示你用户名为admin.接下来用burp对密码进行爆破,发现弱口令0123456 ...

  4. 转《Python爬虫学习系列教程》学习笔记

    http://www.cnblogs.com/xin-xin/p/4297852.html

  5. firewalld学习--service

    service是firewalld中另外一个非常重要的概念.还是拿门卫的例子来解释. 在iptables的时代我们给门卫下达规则时需要告诉他“所有到22号楼的人全部予以放行”.“所有到80号楼的人全部 ...

  6. Day4 - L - Tram POJ - 1847

    Tram network in Zagreb consists of a number of intersections and rails connecting some of them. In e ...

  7. 044、Java中逻辑运算之向左边移位2位实现功能

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  8. input中name和id的区别

    一直很困惑,表单里面input标签有id和name,它们之间到底有什么区别自己很少去想,只知道一般的场景该怎么使用,今天就在网上搜索了一下,自己也总结一下.为什么有了ID还要有Name呢?其实ID就像 ...

  9. 数据库建模工具pd的使用

  10. VMWare 禁用vmem虚拟内存文件

    使用 VMWare 虚拟机,虚拟机启动后,会在虚拟机目录下建立一个与虚拟内存大小相同的 .vmem文件 这个文件主要是将虚拟机内存的内容映射到磁盘,以支持在虚拟机的暂停等功能 对所有的虚拟机" ...