UVA 11609 Teams 组合数学+快速幂

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 ?nd in how many ways a coach
can select a team from his N players. Remember that, teams with same players but having di?erent
captain are considered as di?erent team.
Input
The ?rst 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,n个人,标号为1~n,现在选若干人组成一队,并且选出一个队长,问说可以选多少种队伍,队长,人数,成员不同均算不同的队伍。

题解:我们枚举选择k个人(1<=k<=n)

答案就是: 1*c(n,1)+2*c(n,2)+.......+n*c(n,n);

   接着提出n

    化为:    n*(c(n-1,0)+c(n-1,1)+c(n-1,2)+......+c(n-1,n-1));

  答案就是:n*(2^(n-1));快速幂求解

//meek///#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <vector>
using namespace std ;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
typedef long long ll;

const int N = 110;
const int inf = 99999999;
const int MOD= 1000000007;

ll quick_pow(ll x,ll p) {
    if(!p) return 1;
    ll ans = quick_pow(x,p>>1);
    ans = ans*ans%MOD;
    if(p & 1) ans = ans*x%MOD;
    return ans;
}
int main() {
    int T,cas=1;
    ll n;
    scanf("%d",&T);
    while(T--) {
        scanf("%lld",&n);
        printf("Case #%d: %lld\n",cas++,n*quick_pow(2,n-1)%MOD);
    }
    return 0;
}

DAIMA

    

时间: 2024-10-09 11:15:24

UVA 11609 Teams 组合数学+快速幂的相关文章

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=(a

UVA11609 - Teams(组合数学+快速幂)

题目链接 题意:从N个人中选出K个人为一只队伍(1 <= K <= N),每个队伍都要有一个队长,当队长不同时,所代表的队伍也不同,求一共可以选出多少只队伍. 思路:依题目可得ans = sum(i * C(i, n)),化简可得ans = n * sum(C(i, n - 1)) = n * 2 ^ (n - 1).之后用快速幂求解. 代码: #include <iostream> #include <cstdio> #include <cstring>

UVa 11609 组队(快速幂)

https://vjudge.net/problem/UVA-11609 题意: 有n个人,选一个或多个人参加比赛,其中一名当队长,有多少种方案?如果参赛者完全相同,但队长不同,算作不同的方案. 思路: 之后就是快速幂处理. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<vector> 6 #include&

Uva 11609 - Team ( 组合数学 + 二项式性质 + 快速幂取模 )

Uva 11609 - Team ( 组合数学 + 二项式性质 + 快速幂取模 ) 题意: 有N个人,选一个或多个人参加比赛,其中一名当队长,有多少种方案? (如果参赛者完全相同但是队长不同,也算是一种情况) [ 1<=n <= 10^9 ] 分析: 这题要用到组合式公式的性质 转化之后快速幂取模轻松搞定之 代码: //Uva 11609 - Team /* 组合数公式 + 二项式系数性质 + 快速幂 手动自己推 -> F[n] = C(n,1)*1 + C(n,2)*2 + C(n,n

UVa 11609 - Teams

题目:n个人中取出k个人组成一个小组,并且其中有一名组长,问有多少种取法. 分析:分治.组合数学.F(n) = C(n,1)*1 + C(n,2)*2 + ... = sum(C(n,i)*i) 推导:C(n,i)*i=n*...*(i+1)/ [i*(i-1)*...*1] * i=n * [(n-1)*...*i]/ [i*...*1]=C(n-1,i)*n F(n)= n*sum(C(n-1,i))= n * 2^(n-1) 利用快速幂求解即可. 说明:使用long long防止溢出:用%

UVA - 11609 Teams (排列组合数公式)

In a galaxy far far awaythere is an ancient game played among the planets. The specialty of the game isthat there is no limitation on the number of players in each team, as long asthere is a captain in the team. (The game is totally strategic, so som

HDOJ--4869--Turn the pokers【组合数学+快速幂】

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4869 题意:有m张扑克,开始时全部正面朝下,你可以翻n次牌,每次可以翻xi张,翻拍规则就是正面朝下变背面朝下,反之亦然,问经过n次翻牌后牌的朝向有多少种情况. 这道题在比赛时我们只开了个头,却无从下手.我看了网上的解题报告,说的都比较简单,对于我这名菜鸟来说也想了比较长的时间才想明白,所以我想写的清楚一些日后再看还能看的很清晰. 思路是这样,每张牌翻奇数次必然是正面朝上,翻偶数次则还是正面朝下.现在用0

UVA 10870 - Recurrences(矩阵快速幂)

UVA 10870 - Recurrences 题目链接 题意:f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), for n > d. 已知前d项求第n项 思路:矩阵快速幂,对应矩阵为 |a1 a2 a3 ... ad| |1 0 0 ... 0 0 0| |0 1 0 ... 0 0 0| |0 0 1 ... 0 0 0| |0 0 0 ... 0 0 0| |0 0 0 ... 1 0 0| |0 0 0

BZOJ_1008_[HNOI2008]_越狱_(简单组合数学+快速幂)

描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1008 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果 相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱. p.s.我真的没有企图概括的必要... 分析 所有情况是m^n,不可能发生越狱的情况是m*(m-1)^(n-1). 最后答案就是: m*(m^(n-1)-(m-1)^(n-1)).做个快速幂就好了. 注意: 1.