Uva 11609 Teams (组合数学)

题意:有n个人,选不少于一个人参加比赛,其中一人当队长,有多少种选择方案。

思路:我们首先C(n,1)选出一人当队长,然后剩下的 n-1 人组合的总数为2^(n-1),这里用快速幂解决

代码:

#include <iostream>
#define ll long long
using namespace std;
const ll mod = 1000000007;

ll qmod(ll a, ll b)
{
    ll ans=1;
    while(b)
    {
        if(b&1)
        {
            ans=(ans*a)%mod;
        }
        b=b/2;
        a=(a*a)%mod;
    }
    return ans;
}

int main()
{
    int t,i=0;
    cin>>t;
    while(t--)
    {
        ll n;
        i++;
        cin>>n;
        ll ans=n%mod;
        ans=(n*qmod(2,n-1))%mod;
        cout<<"Case #"<<i<<": ";
        cout<<ans<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/simplekinght/p/6161051.html