您的位置:首页 > 其它

Lucky Numbers(排列问题)(数学)(n个数排列且可重复用,元素个数不定)(等比数列)

2016-03-11 11:28 453 查看
Lucky Numbers(排列问题)(数学)(n个数排列且可重复用,元素个数不定)

Lucky Numbers
Crawling in process...
Crawling failed
Time Limit:500MS    
Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit

Status

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
2


Output
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: