HDU 1023 Train Problem II
Train Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2830 Accepted Submission(s): 1562
Problem Description
As we all know the Train Problem I, the boss
of the Ignatius Train Station want to know if all the trains come in
strict-increasing order, how many orders that all the trains can get out
of the railway.
Input
The input contains several test cases. Each
test cases consists of a number N(1<=N<=100). The input is
terminated by the end of file.
Output
For each test case, you should output how many ways that all the trains can get out of the railway.
Sample Input
1
2
3
10
Sample Output
1
2
5
16796
卡特兰数 大数乘除
Java版本
import java.util.*; import java.io.*; import java.math.*; public class Main { static Scanner cin=new Scanner(System.in); static PrintWriter cout=new PrintWriter(System.out,true); public static void main(String[] args) { BigInteger a[]=new BigInteger[110]; BigInteger b=BigInteger.valueOf(4); BigInteger d=BigInteger.valueOf(2); a[1]=BigInteger.ONE; for(int i=2;i<=100;i++) { BigInteger c=BigInteger.valueOf(i); BigInteger e=BigInteger.valueOf(i+1); a[i]=a[i-1].multiply(b.multiply(c).subtract(d)).divide(e); } while(cin.hasNext()) { int n=cin.nextInt(); System.out.println(a[n].toString()); } } }
c++版本
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <queue> #include <cstdlib> #include <iomanip> #include <cmath> #include <ctime> #include <map> #include <set> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define pi acos(-1.0) #define ei exp(1) #define PI 3.141592653589793238462 #define ios() ios::sync_with_stdio(false) #define INF 1044266558 #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; //f[n]=2*(2*n-1)*f[n-1]/(i+1); int a[101][101],n; void multiply(int *a,int k,int b) { int ans=0; for(int i=k-1;i>=0;i--) { ans+=a[i]*b; a[i]=ans%10000; ans/=10000; } } void divide(int *a,int k,int b) { int ans=0; for(int i=0;i<k;i++) { ans*=10000; ans+=a[i]; a[i]=ans/b; ans%=b; } } void init() { memset(a,0,sizeof(a)); a[1][99]=1; for(int i=2;i<=100;i++) { memcpy(a[i],a[i-1],sizeof(a[i-1])); multiply(a[i],100,4*i-2); divide(a[i],100,i+1); } } int main() { init(); int i; while(scanf("%d",&n)!=EOF) { for(i=0;i<100 && !a[n][i];i++); printf("%d",a[n][i++]); for(;i<100;i++) printf("%04d",a[n][i]); printf("\n"); } return 0; }