紫书例题p245

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 ≤ 2 K.

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

题意:一开始有一个红气球,每小时,一个红气球会变成3个红气球和1个蓝气球,而一个蓝气球会变成4个蓝气球,如图所示,经三小时变化后。

根据图中给出的气球的分裂方式,求第K次分裂后,第A行到第B行的红色气球的数量。

可以这么想,用前B行的红色气球的数量减去A-1行的红色球的数量就可以得到第A行到第B行的红色气球的数量。

然后再观察第三小时和第二小时的图,发现第二小时的图和第三张图分成四块后的其中三块相同,而不相同的一块全是蓝色,不需要计算。

假设函数f(k,i)表示第k小时,前i行的所有红球个数,则问题的答案就是f(k,B)-f(k,A-1)。f(k,i)的求解需要根据i与2的(k-1)次方的大小分类讨论,递归求解。

#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
int T,k,a,b;
long long c[];
long long f(int k, int i)
{
if(!i) return ;
if(!k) return ;
if(i<<<(k-))
return *f(k-,i);
else
return f(k-,i-(<<(k-)))+*c[k-]; <<(k-)=2的k-1次方
}
int main()
{
c[]=;
for(int i=;i<; i++)
c[i]=*c[i-];
cin>>T;
for(int s=;s<=T; s++)
{
cin>>k>>a>>b;
long long total=f(k,b)-f(k,a-);
printf("Case %d: %lld\n",s,total);
}
return ;
}

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

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

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

  2. UVA - 12627 Erratic Expansion(奇怪的气球膨胀)(递归)

    题意:问k小时后,第A~B行一共有多少个红气球. 分析:观察图可发现,k小时后,图中最下面cur行的红气球个数满足下式: (1)当cur <= POW[k - 1]时, dfs(k, cur) ...

  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. eclipse中的项目导出成Androidstudio的识别的项目,so文件打包不进去

    需要加入jniLibs.srcDirs = ['libs']才可以把so文件打入包内 sourceSets { main { manifest.srcFile 'AndroidManifest.xml ...

  2. 饭卡 (背包01 一维数组) http://acm.hdu.edu.cn/showproblem.php?pid=2546

    /* 从一组数据中选出n个数,使这n个数的和最接近一个值x, 背包问题, 从一系列菜中,从最贵的菜(MAX)之外中选出几个菜,使菜的总价格sum最接近money-5:money-sum-MAX; 钱数 ...

  3. 迭代加深搜索算法总结 + Editing a Book UVa11212题解

    迭代加深搜索算法: 对于可以用回溯法解决,但是解答树结点数大的恐怖的问题的一种解决办法,有的问题甚至用bfs连一层节点都遍历不完就超时了.具体方法就是依次枚举搜索层数,从1到一个上限. 结构: int ...

  4. 软件设计模式 B卷

            软件设计模式 试 卷(作业考核 线上)  B  卷   学习中心:            院校学号:             姓名                (共        页 ...

  5. Angular2学习

    1.新建项目 2.新建Model public class TodoItem { public int Id { get; set; } public string Key { get; set; } ...

  6. Swoole源代码学习记录(十五)——Timer模块分析

    swoole版本号:1.7.7-stable Github地址:点此查看 1.Timer 1.1.swTimer_interval_node 声明: // swoole.h 1045-1050h ty ...

  7. JQuery的Ajax跨域请求的

    JQuery的Ajax跨域请求的(Ajax) 什么是jsonp格式呢?API原文:假设获取的数据文件存放在远程server上(域名不同.也就是跨域获取数据),则须要使用jsonp类型.使用这样的类型的 ...

  8. Queueing in the Linux Network Stack !!!!!!!!!!!!!!!

    https://www.coverfire.com/articles/queueing-in-the-linux-network-stack/ Queueing in the Linux Networ ...

  9. 在 Java 中高效使用锁的技巧--转载

    竞争锁是造成多线程应用程序性能瓶颈的主要原因 区分竞争锁和非竞争锁对性能的影响非常重要.如果一个锁自始至终只被一个线程使用,那么 JVM 有能力优化它带来的绝大部分损耗.如果一个锁被多个线程使用过,但 ...

  10. Python开发【第十篇】:CSS --无内容点击-不进去(一)

    Python开发[第十篇]:CSS  --无内容点击-不进去(一)