uva 12627 - Erratic Expansion
2015-08-14 21:51
246 查看
题目:
Piotr found a magical box in heaven. Its magic power is that if you place any red balloon inside itthen, 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 blueballoons. 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 thefollowing diagram.
As you can see, a red balloon in the cell (i,j) (that is
i-th row and
j-th column) will multiply toproduce 3 red balloons in the cells (i∗2−1,j
∗2−1),
(i∗2−1,j
∗2), (i∗2,j
∗2−1)
and a blueballoon in the cell (i
∗ 2, j
∗ 2). Whereas, a blue balloon in the cell (i, j)
will multiply to produce 4 blueballoons 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 sizedoubles (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 wouldlike 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 casecontains 3 integers
K,
A and
B. The meanings of these variables are mentioned above.
K will be inthe range [0,
30] and 1
≤ A
≤ B
≤ 2K
.
Output
For each case, output the case number followed by the total number of red balloons in rows [A, B]
afterK-th hour.
Sample Input
3011318337
Sample Output
此题必须得观察到图的特性,就是对称性。然后观察图就能做出来了。
//
// main.cpp
// uva 12627 - Erratic Expansion
//
// Created by XD on 15/8/14.
// Copyright (c) 2015年 XD. All rights reserved.
//
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<vector>
#include <string.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cstdio>
#define ll long long
using namespace std ;
ll arr2[31] ;
ll arr3[31] ;
ll ans ;
//k表示第几个小时,A表示线
ll kA(ll k , ll A)
{
if (A == 0) {
return 0 ;
}
if (k == 0) {
return 1 ;
}
if (arr2[k-1] < A) {
return 2 * arr3[k-1] + kA(k-1, A- arr2[k-1]) ;
}
else if(arr2[k-1]==A) {
return 2 * arr3[k-1] ;
}
else{
return 2 * kA(k - 1, A) ;
}
}
int main(int argc, const char * argv[]) {
int k , a , b,T,casenum = 0 ;
scanf("%d" ,&T) ;
arr2[0] = 1 ;arr3[0] = 1 ;
for (int i = 1 ; i < 31; i++) {
arr2[i] = 1<<i ;
arr3[i] = 3 * arr3[i-1] ;
}
while (T--) {
scanf("%d%d%d" ,&k ,&a , &b) ;
ans = kA(k, b)- kA(k, a-1) ;
printf("Case %d: %lld\n" ,++casenum ,ans ) ;
}
return 0;
}
Piotr found a magical box in heaven. Its magic power is that if you place any red balloon inside itthen, 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 blueballoons. 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 thefollowing diagram.
As you can see, a red balloon in the cell (i,j) (that is
i-th row and
j-th column) will multiply toproduce 3 red balloons in the cells (i∗2−1,j
∗2−1),
(i∗2−1,j
∗2), (i∗2,j
∗2−1)
and a blueballoon in the cell (i
∗ 2, j
∗ 2). Whereas, a blue balloon in the cell (i, j)
will multiply to produce 4 blueballoons 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 sizedoubles (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 wouldlike 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 casecontains 3 integers
K,
A and
B. The meanings of these variables are mentioned above.
K will be inthe range [0,
30] and 1
≤ A
≤ B
≤ 2K
.
Output
For each case, output the case number followed by the total number of red balloons in rows [A, B]
afterK-th hour.
Sample Input
3011318337
Sample Output
Case 1: 1 Case 2: 27 Case 3: 14
此题必须得观察到图的特性,就是对称性。然后观察图就能做出来了。
//
// main.cpp
// uva 12627 - Erratic Expansion
//
// Created by XD on 15/8/14.
// Copyright (c) 2015年 XD. All rights reserved.
//
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<vector>
#include <string.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cstdio>
#define ll long long
using namespace std ;
ll arr2[31] ;
ll arr3[31] ;
ll ans ;
//k表示第几个小时,A表示线
ll kA(ll k , ll A)
{
if (A == 0) {
return 0 ;
}
if (k == 0) {
return 1 ;
}
if (arr2[k-1] < A) {
return 2 * arr3[k-1] + kA(k-1, A- arr2[k-1]) ;
}
else if(arr2[k-1]==A) {
return 2 * arr3[k-1] ;
}
else{
return 2 * kA(k - 1, A) ;
}
}
int main(int argc, const char * argv[]) {
int k , a , b,T,casenum = 0 ;
scanf("%d" ,&T) ;
arr2[0] = 1 ;arr3[0] = 1 ;
for (int i = 1 ; i < 31; i++) {
arr2[i] = 1<<i ;
arr3[i] = 3 * arr3[i-1] ;
}
while (T--) {
scanf("%d%d%d" ,&k ,&a , &b) ;
ans = kA(k, b)- kA(k, a-1) ;
printf("Case %d: %lld\n" ,++casenum ,ans ) ;
}
return 0;
}
相关文章推荐
- ImageView scaleType
- Android Studio 常见问题汇总
- 华为OJ(最长公共子串及公共最长子序列)
- 信号量以及互斥锁以及自旋锁详细解析
- 提取不重复的整数
- ACM 01背包HDU 1203
- Gym - 100685F Flood BFS
- poj 3264 -Balanced Lineup (RMQ与线段树两种做法)
- HDU-3342 Legal or Not(拓扑排序)
- 容斥定理 hdu1796 How many integers can you find
- 名字的漂亮度
- HDU 5384 Danganronpa (2015年多校比赛第8场)
- S2SH新手框架建立具体过程
- 跳表 SkipList
- HDOJ 1021 Fibonacci Again 【数论】
- HDOJ 1021 Fibonacci Again 【数论】
- Hbernate中级联查询(二)
- chrome效率提升
- POJ数学题目
- 深度学习文献阅读笔记(2)