UVa 11609 - Teams (组合数学)

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 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

3

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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值