uva12627 Erratic Expansion (递归)
来源:互联网 发布:萧山网络歌手大赛2011 编辑:程序博客网 时间:2024/06/05 23:50
Erratic Expansion
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
这题只要求我们看红色气球的数量,我们把每行的红色气球数量竖着写下来,很容易就可以发现这张图形的变化规律,即每次图形上方复制出一个图形,上面每行的数量是下面每行的两倍,原来的图形的数量不变。这里需要利用递归的思想来写。
#include <cstdio>#include <iostream>using namespace std;int k;int Pow(int a,int b){ int base =a; int ans = 1; while(b!=0) { if(b&1) ans*=base; b>>=1; base*=base; } return ans;}long long c(int k){ if(k==0)return 1; else return 3*c(k-1);}long long f(int k,int i){ if(i<=0) return 0; if(k==0) return 1; if(i>=Pow(2,k-1)) return (f(k-1,i-Pow(2,k-1))+2*c(k-1)); else return 2*f(k-1,i);}int main(){ int t; cin>>t; int k,a,b; int kase=0; while(t--) { scanf("%d %d %d",&k,&a,&b); long long ans =f(k,b)-f(k,a-1); printf("Case %d: %lld\n",++kase,ans); } return 0;}
- uva12627 Erratic Expansion (递归)
- UVA12627 Erratic Expansion (递归找规律)
- UVA12627 Erratic Expansion
- uva12627 erratic expansion
- UVA12627 Erratic Expansion
- uva12627 Erratic Expansion
- UVA12627:Erratic Expansion(奇怪的气球膨胀)
- Uva12627 Erratic Expansion【分治】【例题8-12】
- uva 12627 Erratic Expansion 递归
- UVa12674 Erratic Expansion (递归+规律)
- uva 12627 - Erratic Expansion(递归求解)
- uva 12627 - Erratic Expansion 找规律,递归
- UVA 12627 Erratic Expansion (递归,分治)
- UVA 12627:Erratic Expansion(递归)
- uva12627 - Erratic Expansion 入门经典II 第八章 例题8-12
- 算法竞赛入门经典 例题8-12 奇怪的气球膨胀 Erratic Expansion uva12627
- 例题8-12 奇怪的气球膨胀(Erratic Expansion, UVa12627)
- 【高效算法设计——递归】 UVa 12627 Erratic Expansion
- php 冒泡排序
- 工欲善其事必先利器(二)——IDE——IntelliJ IDEA学习指南
- Ubuntu各个版本的镜像下载地址
- JDK的安装及环境变量的配置
- codeforces 611A New Year and Days
- uva12627 Erratic Expansion (递归)
- 代码传递信息方式的探究
- python3 property调用私有方法用法
- HDU-6000 Wash 贪心 队列
- 【TP】产线检测CTP单体异常
- J: Our Journey of Dalian Ends
- Kafka 简析
- BZOJ1455 罗马游戏 【左偏树】
- Tomcat修改端口号