10884 - Persephone

困扰了很久, 偶尔看到了公式, 记下。

Expanding the generating function shows that the number of convex polyominoes having perimeter 2n+8 is given by

 p_(2n+8)=(2n+11)4^n-4(2n+1)(2n; n),  

where p_4=1, p_6=2, and (n; k) is a binomial coefficient。

公式来源于这里: http://mathworld.wolfram.com/ConvexPolyomino.html

View Code
 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 
 4 
 5 public class Main {
 6 
 7     static BigInteger mypow(int n, int k) {
 8         BigInteger ans = BigInteger.ONE;
 9         BigInteger N = BigInteger.valueOf(n);
10         for(; k != 0; k >>= 1) {
11             if((k & 1) != 0) ans = ans.multiply(N);
12             N = N.multiply(N);
13         }
14         return ans;
15     }
16     static BigInteger C(int n, int k) {
17         BigInteger ans = BigInteger.ONE;
18         for(int i = 0; i < k; ++i) {
19             ans = ans.multiply(BigInteger.valueOf(n - i)).divide(BigInteger.valueOf(i + 1));
20         }
21         return ans;
22     }
23     static BigInteger solve(int n) {
24         BigInteger A = BigInteger.valueOf(2 * n + 11).multiply(mypow(4, n));
25         //System.out.println("A = " + A);
26         BigInteger B = BigInteger.valueOf(4 * (2 * n + 1)).multiply(C(2 * n, n));
27         return A.subtract(B);
28     }
29     public static void main(String[] args) {
30         Scanner in = new Scanner(System.in);
31         int T = in.nextInt();
32         for(int kase = 1; kase <= T; ++kase) {
33             int n = in.nextInt();
34             System.out.print("Case #" + kase + ": ");
35             if(((n & 1) != 0) || (n == 2)) {
36                 System.out.println("0");
37             } else if(n == 4) {
38                 System.out.println("1");
39             } else if(n == 6) {
40                 System.out.println("2");
41             } else if(n == 8) {
42                 System.out.println("7");
43             } else {
44                 n = (n - 8) / 2;
45                 System.out.println(solve(n));
46             }
47         }
48 
49     }
50 
51 }

 

posted @ 2013-02-04 22:21  ACSeed  Views(125)  Comments(0)    收藏  举报