UVa11609 - Teams(快速求幂)

来源:互联网 发布:数据分析范例 编辑:程序博客网 时间:2024/06/09 16:04

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 sometimesless player increases the chance to win). So the coaches who have a total of Nplayers to play, selects K (1 ≤ K ≤ N) players and makeone 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 hisN players.Remember that, teams with same players but having different captain areconsidered as different team.

 

Input

 

Thefirst line of input contains the number of test cases T ≤ 500.Then each of the next T lines contains the value ofN (1 ≤ N ≤10^9), the number of players the coach has.

 

Output

 

Foreach line of input output the case number, then the number of ways teams can beselected. You should output the result modulo 1000000007.

 

Forexact formatting, see the sample input and output.

 

 

 

Sample Input                                                                               Outputfor Sample Input

3

1

2

3

Case #1: 1

Case #2: 4

Case #3: 12

 

 

计算公式 为f(n) = n * pow(2, n -1) % 1000000007 ,用快速求幂算法

import java.io.FileInputStream;import java.io.InputStreamReader;import java.io.BufferedReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.io.StreamTokenizer;public class Main {public static final boolean DEBUG = false;public StreamTokenizer tokenizer;public BufferedReader cin;public PrintWriter cout;public long n;public void init() {try {if (DEBUG) {cin = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\OJ\\uva_in.txt")));} else {cin = new BufferedReader(new InputStreamReader(System.in));}cout = new PrintWriter(new OutputStreamWriter(System.out));tokenizer = new StreamTokenizer(cin);} catch (Exception e) {e.printStackTrace();}}public String next() {try {tokenizer.nextToken();if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;else if (tokenizer.ttype == StreamTokenizer.TT_WORD) return tokenizer.sval; else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) return String.valueOf((int)tokenizer.nval);else return null;} catch (Exception e) {e.printStackTrace();return null;}}public long f(long a, long b, long n){long product = 1;while (b != 0) {if ((b & 1) != 0) {product = product * a % n;}a = (a % n) * (a % n) % n;b >>= 1;}return product;}public boolean input(){n = Long.parseLong(next());return true;}public void solve(int cas) {long ans = f(2, n - 1, 1000000007);ans = ans * n % 1000000007;cout.println("Case #" + cas + ": " + ans);cout.flush();}public static void main(String[] args) {Main solver = new Main();solver.init();int t= Integer.parseInt(solver.next());for (int i = 1; i <= t; i++) {solver.input();solver.solve(i);}}}




0 0
原创粉丝点击