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
2
6
题意:在题目给定的n个数字长度,用数字7、8最多能组成多少种不同的组合,每组组合的元素个数不定,但不能超出n个数。
如:n=2;
所有情况如下:
77
78
7
87
88
8
共计6种组合。
思路:刚开始想用深搜(dfs()),可是由于每组中的数字可以重复多次使用,感觉用深搜解决不了。于是想有没有别的方法。后来想到拆分,分成几种情况,分别求,然后再求和。
由于组合中元素个数不定,所以可以分成n种情况。
(1) 1, 第一种情况只有1个元素,每个元素可以有两种选择7或8, 有2^1种组合
(2) 2 第二种情况只有2个元素,每个元素可以有两种选择7或8, 有2^2种组合
(3) 3 第三种情况只有3个元素,每个元素可以有两种选择7或8, 有2^3种组合
...............
(n) n 第n种情况有n个元素,每个元素可以有两种选择7或8, 有2^n种组合
ans=2^1+2^2+2^3+……2^n=2^(n+1)-2 (等比数列求和)
My solution:
/*2016.3.10*/
#include<stdio.h>
int n;
long long ans;//n<55,2^55超出int范围,用long long
void gcd()
{
int t;
long long time=2;
t=n+1;
while(t)
{
if(t%2)
ans*=time;
t/=2;
time*=time;
}
ans-=2;
}
int main()
{
int i,j;
while(scanf("%d",&n)==1)
{
ans=1;
gcd();
printf("%I64d\n",ans);
}
return 0;
}