[TOJ] 1179 Game of Connections — catalan數,JAVA檔案輸入輸出

來源:互聯網
上載者:User

給每個點編號為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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.