UVA 12627 Erratic Expansion

https://vjudge.net/problem/UVA-12627

题目

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 ast 2 - 1, j ast 2 - 1)$, $(i ast 2 - 1, j ast 2)$, $(i ast 2, j ast 2 - 1)$ and a blue balloon in the cell $(i ast 2, j ast 2)$. Whereas, a blue balloon in the cell $(i, j)$ will multiply to produce 4 blue balloons in the cells $(i ast 2 - 1, j ast 2 - 1)$, $(i ast 2 - 1, j ast 2)$, $(i ast 2, j ast 2 - 1)$ and $(i ast 2, j ast 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 leq A leq B leq 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

 题解

心情不太好,不解释了

AC代码

#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include<bits/stdc++.h>
using namespace std;
#define REP(r,x,y) for(register int r=(x); r<(y); r++)
#define REPE(r,x,y) for(register int r=(x); r<=(y); r++)
#ifdef sahdsg
#define DBG(...) printf(__VA_ARGS__)
#else
#define DBG(...)
#endif

#define MAXN 10000007
typedef long long LL;

template <class T>
inline void read(T& x) {
	char c=getchar();
	int f=1;
	x=0;
	while(!isdigit(c)&&c!='-')c=getchar();
	if(c=='-')f=-1,c=getchar();
	while(isdigit(c)) {
		x=x*10+c-'0';
		c=getchar();
	}
	x*=f;
}
template <class T>
void write(T x) {
	if(x<0) {
		putchar('-');
		write(-x);
		return;
	}
	if(x>9) write(x/10);
	putchar(x%10+'0');
}

long long f1[31]={1,3};
inline void getf1() {
	REPE(i,2,30) {
		f1[i]=f1[i-1]*3;
	}
}
inline long long c(int i) {
	return 1<<i;
}

inline long long f(int k, int i) {
	if(k==0) {
		if(i<=1) return 1;
		else return 0;
	}
	if(i<=c(k-1))
		return f(k-1,i)*2+f1[k-1];
	else if(i<=c(k))
		return f(k-1,i-c(k-1));
	else return 0;
}
int main() {
	#ifdef sahdsg
	freopen("in.txt", "r", stdin);
	#endif
	getf1();
	int n;
	read(n);
	
	REPE(i,1,n) {
		int k,a,b;
		read(k);read(a);read(b);
		printf("Case %d: ", i);
		write(f(k,a) - f(k,b+1)); putchar('
');
	}
	return 0;
}
原文地址:https://www.cnblogs.com/sahdsg/p/10490262.html