Train Access Stacks Java

Source: Internet
Author: User
Tags pow

The title describes a train of n carriages, sequentially numbered,..., N. Each compartment has two ways of movement, into the stack and out of the stack, asked how many of the possible arrangement of the N-section car stack.

Enter a number, n (n<=60000)

The output of a number s indicates the possible arrangement of the stack of n carriages:

This is going to take a large number, Java

Then Cattleya number: There is a mathematical model s[i]=c (N,2N)/(n+1)

Combinatorial number formula: This is always unable to remember the problem can not be directly asked, will tle. It is necessary to find out how many times each factorization of the result is power, and then use the fast power, and then multiply all the results of exponentiation. Http://www.cnblogs.com/sciorz/p/9263122.html
Importjava.math.*;ImportJava.util.*; Public classMain {//The sieve method to calculate prime number    Static Final intmaxn=120050; Static Boolean[]isprime=New Boolean[MAXN]; Static int[] Prime=New int[MAXN]; Static intPz=0; Static voidGetprime () { for(inti = 2; i < MAXN; ++i) isprime[i]=true;  for(inti = 2; i < MAXN; ++i) {if(Isprime[i]) prime[pz++]=i;  for(intj = 0; J < pz&& (Long) i*prime[j]<maxn; ++j) {Isprime[i*prime[j]]=false; if(i%prime[j]==0) Break; }        }    }    StaticScanner cin=NewScanner (system.in); Static intN=0;  Public Static voidMain (string[] args) {n=Cin.nextint ();        Getprime (); BigInteger ans=b (1);  for(inti = 0; I < pz&&prime[i]<=2*n; ++i) {intcnt=0,p=Prime[i]; intT=n*2;  while(t>0) {//(2n! )cnt+=t/p; T/=p; } t=N;  while(t>0) {//(n! *n! )Cnt-=t/p*2; T/=p; } ans=ans.multiply (POW (p,cnt)); } System.out.println (Ans.divide (B (n+1))); }    StaticBigInteger Pow (intXintN) {//Fast ProductBigInteger ans=b (1), base=B (x);  while(n>0) {            if(n%2==1) ans=ans.multiply (base); Base=base.multiply (base); N>>=1; }        returnans; }    StaticBigInteger B (intx) {returnbiginteger.valueof (x); }}
View Code

Train Access Stacks Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.