UVA 11609 Teams
2016-07-22 20:17
344 查看
Question:
In a galaxy far far away there is an ancient game played among the planets. The specialty of the game
is that there is no limitation on the number of players in each team, as long as there is a captain in
the team. (The game is totally strategic, so sometimes less player increases the chance to win). So the
coaches who have a total of N players to play, selects K (1 ≤ K ≤ N) players and make one of them
as the captain for each phase of the game. Your task is simple, just find in how many ways a coach
can select a team from his N players. Remember that, teams with same players but having different
captain are considered as different team.
Input
The first line of input contains the number of test cases T ≤ 500. Then each of the next T lines contains
the value of N (1 ≤ N ≤ 109
), the number of players the coach has.
Output
For each line of input output the case number, then the number of ways teams can be selected. You
should output the result modulo 1000000007.
For exact formatting, see the sample input and output.
Sample Input
3
1
2
3
Sample Output
Case #1: 1
Case #2: 4
Case #3: 12
题意大意:有n个人,教你选出其中一人或多人去参加比赛,并选出一名队长,让你计算有多少种方案(如果人一样,但队长不同为一种新的方案)
思路:如果仔细去思考不难发现其和为sum=i*C(i(上标),n(下标))(1<=i<=n)求和。然后i*C(i,n)=i*n!/(i!(n-i)!)=n((n-1)!/((i-1)!*(n-i)!)=n*c(i-1,n-1)然后再求和等于sum=n*2^(n-1),这样就将问题转化为了一个简单的数学问题。是不是很巧妙。
(http://acm.hust.edu.cn/vjudge/contest/121559#problem/G)
体会:做题时要仔细观察,并将这种组合求和转化为简单的数学问题。
In a galaxy far far away there is an ancient game played among the planets. The specialty of the game
is that there is no limitation on the number of players in each team, as long as there is a captain in
the team. (The game is totally strategic, so sometimes less player increases the chance to win). So the
coaches who have a total of N players to play, selects K (1 ≤ K ≤ N) players and make one of them
as the captain for each phase of the game. Your task is simple, just find in how many ways a coach
can select a team from his N players. Remember that, teams with same players but having different
captain are considered as different team.
Input
The first line of input contains the number of test cases T ≤ 500. Then each of the next T lines contains
the value of N (1 ≤ N ≤ 109
), the number of players the coach has.
Output
For each line of input output the case number, then the number of ways teams can be selected. You
should output the result modulo 1000000007.
For exact formatting, see the sample input and output.
Sample Input
3
1
2
3
Sample Output
Case #1: 1
Case #2: 4
Case #3: 12
题意大意:有n个人,教你选出其中一人或多人去参加比赛,并选出一名队长,让你计算有多少种方案(如果人一样,但队长不同为一种新的方案)
思路:如果仔细去思考不难发现其和为sum=i*C(i(上标),n(下标))(1<=i<=n)求和。然后i*C(i,n)=i*n!/(i!(n-i)!)=n((n-1)!/((i-1)!*(n-i)!)=n*c(i-1,n-1)然后再求和等于sum=n*2^(n-1),这样就将问题转化为了一个简单的数学问题。是不是很巧妙。
(http://acm.hust.edu.cn/vjudge/contest/121559#problem/G)
#include <iostream> #include <cstdio> using namespace std; typedef long long LL; const LL MOD=1e9+7; LL quick_mod(LL a,LL b) { LL res=1; while (b) { if(b&1) res=(res*a)%MOD; b>>=1; a=a*a%MOD; } return res; } int main() { int n,t; cin>>t; for(int i=1;i<=t;i++) { cin>>n; cout<<"Case #"<<i<<": "<<quick_mod(2,n-1)*n%MOD<<endl; } }
体会:做题时要仔细观察,并将这种组合求和转化为简单的数学问题。
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- C#递归算法之分而治之策略
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- C#算法之大牛生小牛的问题高效解决方法
- C#算法函数:获取一个字符串中的最大长度的数字
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- 经典排序算法之冒泡排序(Bubble sort)代码
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法