uva 11609 Teams

本文解析了一个关于在无限远的星系中进行古老游戏的问题,该游戏中教练从N名球员中选择K名球员并指定一名队长,求可能的队伍组合数量。通过数学公式n×2^(n-1),给出了简洁的解答。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题:
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
3
1
2
3
Sample Output
Case #1: 1
Case #2: 4
Case #3: 12

#include <bits/stdc++.h>
using namespace std;
const long mod=1000000007;
long quickmod(long a,long b)
{
    long ans=1;
    while(b)
    {
        if(b&1)
        {
            ans=(ans*a)%mod;
            b--;
        }
        b/=2;
        a=a*a%mod;
    }
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    long n,t,k=1;
    cin>>t;
    while(t--)
    {
        cin>>n;
        long ans=(n%mod)*(quickmod(2,n-1)%mod)%mod;
        cout<<"Case #"<<k++<<": "<<ans<<endl;
    }
    return 0;
}

解答:
非常简答的数学题,公式能够很容易的就列出来等于
0×C(0,n)+1×C(1,n)+2×C(2,n)+….+n×C(n,n)
如果记得这个公式的最后结果可以直接写出来等于n×2^(n-1)
如果不记得,可以直接把组合数展开,然后把n提出来后能够得到结果
或者利用K×C(K,n)=n×C(K-1,n-1)的公式替换。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值