我是個對電腦科學有興趣的學生,我會貼上我的學習歷程及生活心情,也請大大們多多指教。 :)

2015年6月12日 星期五

[UVa] 10303 - How Many Trees?

題目網址: https://goo.gl/Fhbzhn

題意:
(from luckycat)


解法:
大數 & 卡塔蘭數。

TAG: Catalan Number, Math, BigNum

注意:

程式碼:
/**
* Tittle: 10303 - How Many Trees?
* Author: Cheng-Shih, Wong
* Date: 2015/06/11
*/
import java.util.*;
import java.math.*;
public class Main {
public static void main( String[] args ) {
final int N = 1005;
BigInteger[] cat = new BigInteger[N];
Scanner input = new Scanner(System.in);
int n;
// init
cat[1] = BigInteger.ONE;
for( int i=1; i<1000; ++i )
cat[i+1] = cat[i].multiply( BigInteger.valueOf(2*(2*i+1)) ).divide( BigInteger.valueOf(i+2) );
// solve
while( input.hasNext() ) {
n = input.nextInt();
System.out.println(cat[n]);
}
input.close();
}
}

沒有留言:

張貼留言

任何意見都樂意傾聽