給每個點編號為1~2n。寫成圈兒以後,以點1為基準,發現只能連偶數號的點。依次和它們連線,把每次求出的方案數加到結果裡。每連一條線就會把這個圈其餘的點分成兩部分,每部分對應點數的方案數是求過的,兩部分方案數相乘,加到結果裡去。如果跟相鄰的點相連,就把剩下的點串連的方案數直接加進去。
推了一下前幾個,發現正好是catalan數~
Show Code - Run ID 954408
Submit Time:
2010-08-08 19:23:16
Language:
Java
Result:
Accepted
Pid:
1179
Time:
0.09 sec.
Memory:
211128 K.
Code Length:
1.0 K.
import java.io.*;<br />import java.math.*;<br />import java.util.*;<br />public class Main {<br /> public static BigInteger Comb(BigInteger n,BigInteger k){<br /> BigInteger sum = BigInteger.ONE ;<br />if(k.equals(BigInteger.ZERO) ||k.equals(n)) return BigInteger.ONE;<br />if(k.compareTo(n.shiftRight(1)) > 0) k = n.subtract(k);<br /> int i, j = k.intValue() ;<br />for(i = 1; i <= j; i++){<br /> sum = sum.multiply(n);<br /> sum = sum.divide(BigInteger.valueOf(i));<br /> n = n.subtract(BigInteger.ONE);<br /> }<br />return sum;<br /> }<br /> public static void main(String[] args) {<br /> Scanner cin = new Scanner(new BufferedInputStream(System.in));<br /> int num ;<br /> BigInteger n, n2 ;<br /> while(true)<br /> {<br /> num = cin.nextInt() ;<br /> if(num == -1) break ;<br /> n = BigInteger.valueOf(num) ;<br /> n2 = n.shiftLeft(1) ;<br /> System.out.println(Comb(n2, n).divide(n.add(BigInteger.ONE)));<br /> }<br /> }<br />}<br />
然後學了一下JAVA的檔案輸入輸出~import java.io.*;<br />import java.math.*;<br />import java.util.*;<br />import java.lang.* ;<br />public class Main {<br /> public static BigInteger Comb(BigInteger n,BigInteger k){<br /> BigInteger sum = BigInteger.ONE ;<br />if(k.equals(BigInteger.ZERO) ||k.equals(n)) return BigInteger.ONE;<br />if(k.compareTo(n.shiftRight(1)) > 0) k = n.subtract(k);<br /> int i, j = k.intValue() ;<br />for(i = 1; i <= j; i++){<br /> sum = sum.multiply(n);<br /> sum = sum.divide(BigInteger.valueOf(i));<br /> n = n.subtract(BigInteger.ONE);<br /> }<br />return sum;<br /> }<br /> public static void main(String[] args) throws IOException{<br /> Scanner cin = new Scanner(new FileReader("in.txt"));<br /> PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter("out.txt"))) ;<br /> int num ;<br /> BigInteger n, n2 ;<br /> while(true)<br /> {<br /> num = cin.nextInt() ;<br /> if(num == -1) break ;<br /> n = BigInteger.valueOf(num) ;<br /> n2 = n.shiftLeft(1) ;<br /> output.println(Comb(n2, n).divide(n.add(BigInteger.ONE)));<br /> }<br /> cin.close();<br /> output.close();<br /> }<br />}
我用這個程式跑出了所有結果,打表用C一交就排到第一頁去了……hiahia