UVA - 12627 Erratic Expansion(奇怪的气球膨胀)(递归)
题意:问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(奇怪的气球膨胀)(递归)的更多相关文章
- UVA - 12627 Erratic Expansion 奇怪的气球膨胀 (分治)
紫书例题p245 Piotr found a magical box in heaven. Its magic power is that if you place any red balloon i ...
- UVA 12673 Erratic Expansion 奇怪的气球膨胀 (递推)
不难发现,每过一个小时,除了右下方的气球全都是蓝色以外,其他都和上一个小时的气球是一样的,所以是可以递推的.然后定义一类似个前缀和的东西f(k,i)表示k小时之后上面i行的红气球数.预处理出k小时的红 ...
- UVa 12627 Erratic Expansion - 分治
因为不好复制题目,就出给出链接吧: Vjudge传送门[here] UVa传送门[here] 请仔细看原题上的那幅图,你会发现,在时间t(t > 0),当前的气球构成的一幅图,它是由三个时间为( ...
- Uva 12627 Erratic Expansion(递归)
这道题大体意思是利用一种递归规则生成不同的气球,问在某两行之间有多少个红气球. 我拿到这个题,一开始想的是递归求解,但在如何递归求解的思路上我的方法是错误的.在研读了例题上给出的提示后豁然开朗(顺便吐 ...
- UVA 12627 - Erratic Expansion
一个红球能够分裂为3个红球和一个蓝球. 一个蓝球能够分裂为4个蓝球. 分裂过程下图所看到的: 设当前状态为k1.下一状态为k2. k1的第x行红球个数 * 2 ⇒ k2第2*x行的红球个数. k1的第 ...
- uva 12627 - Erratic Expansion(递归求解)
递归的边界条件写的多了--不是必需写呢么多的.. 不明确可共同探讨~ #include<cstdio> #include<iostream> #include<cmath ...
- UVa 12627 奇怪的气球膨胀(分治)
https://vjudge.net/problem/UVA-12627 题意:一开始有一个红气球.每小时后,一个红气球会变成3个红气球和1个蓝气球,而1个蓝气球会变成4个蓝气球.如图所示分别是经过0 ...
- 12627 - Erratic Expansion——[递归]
Piotr found a magical box in heaven. Its magic power is that if you place any red balloon inside it ...
- 【数形结合】Erratic Expansion
[UVa12627]Erratic Expansion 算法入门经典第8章8-12(P245) 题目大意:起初有一个红球,每一次红球会分成三红一蓝,蓝球会分成四蓝(如图顺序),问K时的时候A~B行中有 ...
随机推荐
- JS中的鼠标移入移除监控操作
有些时候我们需要通过页面来监控用户的行为,包括鼠标操作键盘操作,本文章介绍的是鼠标的操作监控: <script> window.onload = function(){ var oDiv ...
- 如何在SecureCRT中上传文件到linux服务器上
1.使用yum安装运行命令sudo yum install lrzsz(默认使没有安装运行命令的) 2.上传命令rz 下载命令sz
- Day3-M-Cable master POJ1064
Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Commi ...
- C# 篇基础知识11——泛型和集合
.NET提供了一级功能强大的集合类,实现了多种不同类型的集合,可以根据实际用途选择恰当的集合类型. 除了数组 Array 类定义在System 命名空间中外,其他的集合类都定义在System.Coll ...
- jumpserver手动配置文档
1.环境 centos7.6 硬盘 200G cpu 8核心 内存 32G (本地测试的时候,有报错,原因为虚拟机配置不够,此时为在pve虚拟化上面做的linux系统) 2.https:/ ...
- SciPy 积分
章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...
- XV6源代码阅读-同步机制
Exercise1 源代码阅读 锁部分:spinlock.h/spinlock.c以及相关其他文件代码 // Mutual exclusion lock. struct spinlock { uint ...
- uboot的环境变量
https://www.cnblogs.com/biaohc/p/6398515.html uboot 环境变量实现原理: 首先我们先要搞清楚uboot中环境变量的作用,uboot中环境变量的作用就是 ...
- Day7 - A - Visible Lattice Points POJ - 3090
A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), othe ...
- 033.SAP上查看IDOC接口,PI接口查不到的日志记录,可能在IDOC接口日志里面
01. SAP系统发料之后,数据没有传输到条码系统,同事也没有任何bc01的日志,这是就要考虑是不是在IDOC接口了,输入事务代码WE02或者WE05 02.双击查看内容 03.点开就能看到详细内容了 ...