2的模幂计算(UVA 11609)

import java.io.*;
import java.util.*;
import java.math.*;

public classMain {
	
	static final int MOD = 1000000007;
	
     //n * (C(n - 1, 0) + C(n - 1, 1) + ... + C(n - 1, n - 1)) == n * 2 ^ (n - 1) {模幂} long BigMod(int n) { long res = 1, temp = 2; while (n > 0) { if ((n & 1) != 0) res = (res * temp) % MOD; n >>= 1; temp = (temp * temp) % MOD; } return res; } void run() { int n, t; t = cin.nextInt(); for (int Case = 1; Case <= t; Case++) { n = cin.nextInt(); System.out.println("Case #" + Case + ": " + (n * BigMod(n - 1)) % MOD); } } public static void main(String[] args) { Mainsolved = new Main(); solved.run(); } // static InputStream inputStream = System.in; // static InputReader cin = new InputReader(inputStream); Scannercin = new Scanner(new BufferedInputStream(System.in)); } classInputReader { private InputStreamstream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStreamstream) { this.stream = stream; } public int read() { if (numChars == -1) return -1; //throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOExceptione) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); if (c == -1) return -1; while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public Stringnext() { StringBuilderstr = new StringBuilder(); int ch; while (isSpaceChar(ch = read())); if (ch == -1) return null; do { str.appendCodePoint(ch); } while (!isSpaceChar(ch = read())); return str.toString(); } public static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public char nextCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } }


公式:
 1.C(n, 1) + 2·C(n, 2) + 3·C(n, 3) + ... + n·C(n, n) = n2n-1.

  2.C(n, k) =

n!
k!(n-k)!

posted on 2013-02-20 10:15  Sure_Yi  阅读(246)  评论(0编辑  收藏  举报

导航