【周练2016.3.7】B - Lucky Numbers(位运算,水)

B - Lucky Numbers

Time Limit:500MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u

Description 
The numbers of all offices in the new building of the Tax Office of IT City will have lucky numbers.

Lucky number is a number that consists of digits 7 and 8 only. Find the maximum number of offices in the new building of the Tax Office given that a door-plate can hold a number not longer than n digits.

Input 
The only line of input contains one integer n (1 ≤ n ≤ 55) — the maximum length of a number that a door-plate can hold.

Output 
Output one integer — the maximum number of offices, than can have unique lucky numbers not longer than n digits.

Sample Input 
Input 

Output 
6



题意:给出变量n,表示最多有n位,这n位只能由7或8组成,问有多少种组成方法。

解题:每一位都可以是7或8,n位的数有2的n次方种,n-1位有2的n-1次方种,依次类推,直到1位(2种)。

    要输出的结果即为2为首项2为公比的前n项和。

    化简后,公式为:2*(2 ² - 1)/ / 科普:上标的输入方法:按住ALT,然后在小键盘输入0178(这里不知道为啥总是变成上标2,改正一下,应该是n)

    化简完还要注意a的范围,要用longlong型。

代码如下:

#include <cstdio>
int main()
{
	long long a;
	a=2;
	int n;
	scanf ("%d",&n);
	a<<=n-1;
	a=(a-1)*2;
	printf ("%lld",a);
	return 0;
}


你可能感兴趣的:(【周练2016.3.7】B - Lucky Numbers(位运算,水))