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

19 篇文章 0 订阅
3 篇文章 0 订阅

Lucky Numbers(排列问题)(数学)(n个数排列且可重复用,元素个数不定)
Lucky Numbers
Crawling in process... Crawling failed 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
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;
 } 




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值