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
Output
题意:在题目给定的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;
}
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;
}
相关文章推荐
- HDU 2604 Queuing(矩阵快速幂)
- 2016蓝桥杯历届往题——蚂蚁感冒
- java 动态代理
- 自动化测试工具QTP的学习
- QQ5.0侧滑效果实现
- android app的签名及加密
- 浅谈C++中的那些内存泄露
- View事件分发机制分析
- 动态设置ListView的高度
- 四:分享和更新项目
- 无需改写已有代码,4步从Objective-C过渡到Swift
- Codeforces Round #344 (Div. 2) B. Print Check
- 解决弹性盒子模型容器因为文字大小而被撑开
- c++第一次上机作业1-1
- Jenkins设置Window编译环境从节点
- c#使用多线程的几种方式示例详解
- 理解全虚拟、半虚拟以及硬件辅助的虚拟化
- 工厂模式,控制反转(依赖注入)
- 快速排序--Java实现
- 58. Length of Last Word