Teams
Input: Standard Input
Output: Standard Output
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 ≤ 10^9), 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 Output for Sample Input
3 1 2 | Case #1: 1 Case #2: 4 Case #3: 12 |
Problem Setter: Towhidul Islam Talukdar
Special Thanks: Md. Arifuzzaman Arif
题意:
有n个人,选一个或多个人参加比赛,其中一名当队长,有多少种方案。
根据组合数学可以得到
ans = C(n,1) + 2*C(n, 2) + 3*C(n, 3) + .. + n*C(n, n)
设
a1 = 0*C(n, 0) + 1*C(n, 1) + .. + i*C(n, i) + .. n*C(n, n)
a2 = n*C(n, n) + (n-1)*C(n, n-1) + .... + 0*C(n, 0)
由组合性质C(n, i) = C(n, n-i)
2*ans = a1 + a2 = n * [ C(n, 0) + C(n, 1) + .. + C(n, n) ] = n * 2^n
则ans = n * 2^(n-1)
开始的时候是先算出n*2^n然后结果/2 结果WA了好多次,改为计算n*2^(n-1)就是对的
后来突然想起来这要取模,所以不能直接除,然后改为乘2的逆,虽然多此一举,但是是为了找出WA的原因
代码也是先计算n*2^n然后/2的版本
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif
#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)
const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));
const int MOD = 1000000007;
LL pow_mod(LL a, LL p) {
if(p == 0) return 1;
LL ret = pow_mod(a, p/2);
ret = (ret * ret) % MOD;
if(p&1) ret = (ret * a) % MOD;
return ret;
}
LL inv(int a) {
return pow_mod(a, MOD-2);
}
int main() {
int T;
scanf("%d", &T);
for(int kase=1; kase<=T; kase++) {
int n;
scanf("%d", &n);
LL ans = pow_mod(2, n);
ans = (ans * n) % MOD;
ans = (ans * inv(2)) % MOD;
printf("Case #%d: %lld\n", kase, ans);
}
return 0;
}