UVA-11609 Teams

来源:互联网 发布:加工中心打孔编程 编辑:程序博客网 时间:2024/06/06 13:10

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

分析:根据组合数的性质,0*C(n,0)+1*C(n,1)+……+n*C(n,n)=n*2^(n-1)。2*10^9较大,应使用快速幂,否则会超时。快速幂就是折半求幂,将每一步的结果进行保存,避免重复求解。

Source:

#include<stdio.h>long long f(int n)                                //快速幂{    long long v;    if(n==0)        return 1;    v=f(n/2);                           //将每一步结果保存在v中    if(n%2==0)        return v*v%1000000007;    else        return (v*v%1000000007*2)%1000000007;}int main(){    int t,n,i;    long long ans;    scanf("%d",&t);    for(i=1;i<=t;i++)    {        scanf("%d",&n);        ans=(n*f(n-1))%1000000007;        printf("Case #%d: %lld\n",i,ans);    }    return 0;}
0 0
原创粉丝点击