An in-depth study on the algorithm of the number of Yang Hui in the nth row of the triangle

Source: Internet
Author: User

Yang Hui triangle:

First of all to popularize some of the knowledge we do not need to know (just want to install the force ...) ).


Yang Hui Triangle, the reason is called Yang Hui Triangle, is because he in the Chinese mathematician Yang Hui a title for "Detailed nine chapter algorithm" appeared in, so posterity named in his name, called Yang Hui Triangle.


The Yang Hui triangle has very many interesting properties:

For example, the sum of the numbers on the nth line equals 2^ (n-1).

Each number equals the sum of the two numbers above him.

The number of the nth row is equal to the number of n-m+1 of the nth row.

The nth row has n number.

......

And so on ....


Today, however, I am not going to talk about these qualities, but rather the most fundamental question-how to quickly know the number of the numbers in the nth row and--suppose there is such a topic, and the data range is as follows:



The One:

As long as the Yang Hui Triangle is the most common one of the number is equal to the upper left corner, the upper right corner of two number of the sum of the nature to simulate, and then with high precision can be 40 points.


The same as:

After getting 40 points, you need to understand one of the most basic Yang Hui trigonometry laws: The number of M in the nth row of the Yang Hui Triangle can be expressed as C (n-1,m-1).


Knowing this simple law, we can calculate the corresponding value of the combination according to this law, and then raise the precision, can achieve about 90 points, but there is still a limit data too.

Code:

var s:string;
        A,C,ANS:ARRAY[1..100000] of Longint;
N,m,i,j,k,len,d:longint;
        Begin READLN (N,M);
                if (m=1) or (m=n) THEN begin WRITELN (1);
        Halt
        End
        Dec (n);
        A[1]:=n;
        Len:=1;
        D:=n;
                For i:=1 to M-2 do begin d:=d-1;
                STR (d,s);
                Fillchar (C,sizeof (c), 0);
                        For j:=1 to Len do begin c[j]:=c[j]+a[j]*d;
                        C[j+1]:=c[j+1]+c[j] DIV 10000;
                C[J]:=C[J] MoD 10000;
                End
                Len:=len+length (s);
                while (c[len]=0) and (len>1) do Dec (len);
        For J:=1 and Len do a[j]:=c[j];
        End
        If m=2 then ans:=a;
                For j:=2 to M-1 do begin d:=0;
                 For I:=len Downto 1 do begin       Ans[i]:= (D*10000+a[i]) Div J;
                D:= (D*10000+a[i]) mod J;
                End
                while (ans[len]=0) and (len>1) do Dec (len);
        For I:=1 and Len do a[i]:=ans[i];
        End
        Write (Ans[len]);  For i:=len-1 Downto 1 does if ans[i]>=1000 then write (Ans[i]) else if ans[i]>=100 then 
Write (' 0 ', ans[i]) else if ans[i]>=10 then write (' xx ', ans[i]) Else write (' ', ans[i]); End.


The three:

To keep improving, you need to know where the problem is. It's obvious that the problem with the one-and-only is that for some number you multiply it, and besides it (or its factor) obviously these are unnecessary operations.

For example:

C (10,6) =10*9*8*7*6*5/6*5*4*3*2*1

Obviously, 6 and 5 of these two numbers do not need to be multiplied at all, so, as for how to achieve, do not post code.


Second, because high-precision even if the pressure, with the operator * To do is still very slow, so we can take a number and then pour the past except a number, multiply the time from the Mahayana, except when the small removal, so that efficiency can be brought up, the limit data of 0.7s.


Four:

We can continue to optimize, but we need to change some methods of calculation, but the nature does not change.


Because we put a lot of numbers in our permutations, but in fact, if we can get all the factors of all the numbers we need to multiply and all the elements we need to divide, we subtract these factors from each other (the number of any one factor of any factor in the multiplier is absolutely not less than the number of factors that are required to be removed) finally , the rest of those factors, we multiply, and then press a bit, the limit data about 0.35s can be over, almost as fast as the way three.

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.