N!
(1)
// To understand the demo, change Max to 4, and use N = 4 as an example <br/> // The last sentence shows that B is used to store the results, from high to low; B [max-1] is a single position, B [max-2] is ten, and so on <br/> // A and B are stored in the same way, however, the <br/> // authors used to store intermediate results do not use multiplication, which is obviously replaced by addition. <Br/> # include "stdafx. H "<br/> # include <iostream> <br/> # define MAX 1000 // in actual applications, this should be changed to a relatively large number, for example, 1000; it indicates that the number of up to 1000 bits can be expressed. <br/> using namespace STD; </P> <p> void print (INT array []) // print <br/> {<br/> for (INT I = 0; I <Max; I ++) <br/> cout <array [I] <""; <br/> cout <Endl; <br/>}</P> <p> long nfai (long n) // evaluate N !, For comparison <br/> {<br/> int res = 1; <br/> for (long I = 2; I <= N; I ++) <br/>{< br/> res = res * I; <br/>}</P> <p> return res; <br/>}< br/> void main () <br/>{< br/> int A [Max], B [Max]; <br/> int I, j, K, L = 0, N; </P> <p> cout <"input a number:" <Endl; <br/> n = 13; // take input 4 as an example. </P> <p> // initialize a, B <br/> for (I = 0; I <max-1; I ++) <br/> {<br/> A [I] = 0; B [I] = 0; <br/>}< br/> A [max-1] = 1; B [max-1] = 1; </P> <p> for (I = 1; I <= N; I ++) // for each number from 1 to n <br/>{< br/> for (j = max-1; j> = 0; j --) // from low to high <br/>{< br/> for (k = 1; k <I; k ++) // If I = 3, in this case, a stores the previous result 3 = 1*2 <br/>{< br/> int f = 0; // indicates the number of places to be entered <br/> while (1) <br/> {<br/> If (B [J] + A [J]-f * 10) <10) // If carry is not enough <br/> {<br/> B [J] = B [J] + A [J]-f * 10; // value of the local digit <br/> break; <br/>}< br/> else // If carry is sufficient <br/> {<br/> B [J-1] = B [J-1] + 1; // Add 1 to the upper position <br/> F ++; <br/>}</P> <p> // cout <"A = "; <br/> // print (a); <br/> // cout <"B ="; <br/> // print (B ); <br/> A [J] = B [J]; // Save the base value to a <br/>}</P> <p> cout <"Number" <I <"processed" <Endl; <br/>}</P> <p> while (B [l] = 0) l ++; // The portion that exceeds the high value of 0 </P> <p> cout <"Result:"; <br/> while (L <max) // This shows that B is used to store the results, from high to low; B [max-1] for a single position, B [max-2] for ten, <br/> {cout <B [l]; l ++ ;}< br/> cout <Endl; </P> <p> cout <nfai (n) <Endl; </P> <p>}
For
For (k = 1; k <I; k ++,
If I = 3, j = max-1,
The new single digit is the number of the first three digits after the result is added and carried. (When the cycle starts, a [max-1], B [max-1] are all the places in the previous results)
(2)
From http://www.programfan.com/blog/article.asp? Id = 568
# Include <iostream> <br/> # include <stack> <br/> # include <fstream> <br/> using namespace STD; <br/> struct node <br/> {<br/> int I; <br/> node * Next; <br/> }; <br/> node * createnode () <br/> {<br/> node * node = new node; <br/> node-> I = 0; <br/> node-> next = NULL; <br/> return node; <br/>}< br/> node * mullink (node * head, int I) <br/>{< br/> node * P = head; <br/> head-> I = 1; <br/> P = head; <br/> int temp = 0; <br/> for (in T j = 1; j <= I; j ++) <br/>{< br/> while (P) <br/>{< br/> P-> I * = J; <br/> P-> I + = temp; <br/> If (p-> I> 9) <br/>{< br/> temp = p-> I/10; <br/> If (! P-> next) <br/> P-> next = createnode (); <br/> P-> I % = 10; <br/> P = p-> next; <br/>}< br/> else <br/> {<br/> temp = 0; <br/> P = p-> next; <br/>}< br/> P = head; <br/>}< br/> return head; <br/>}< br/> void displaylink (node * head) <br/>{< br/> fstream OUTFILE; <br/> stack <int> display; <br/> OUTFILE. open ("example.txt", IOS: Out | IOs: ATE); <br/> while (head) <br/>{< br/> display. push (Head-> I); <br/> head = Head-> next; <br/>}< br/> while (! Display. empty () <br/>{< br/> cout <display. top (); <br/> OUTFILE <display. top (); <br/> display. pop (); <br/>}< br/> cout <Endl; <br/>}< br/> void freelink (node * head) <br/>{< br/> node * P = head; <br/> while (p) <br/>{< br/> head = p-> next; <br/> Delete P; <br/> P = head; <br/>}< br/> int main (INT argc, char * argv []) <br/>{< br/> int N; <br/> cout <"Please input the number" <Endl; <br/> CIN> N; <br/> node * head = createnode (); <br/> mullink (Head, N ); <br/> displaylink (head); <br/> freelink (head); <br/> system ("pause"); <br/> return 0; <br/>}</P> <p>
(3)
From http://download.csdn.net/source/629944
# Include <stdlib. h> <br/> # include <time. h> <br/> # include <stdio. h> <br/> # include <string. h> </P> <p> # define maxsize 100000 <br/> # define Radix 10000 <br/> # define log10_radix 4 </P> <p> void change (long A [], int & M); <br/> int factorial (Long, long a []); </P> <p> int main () <br/>{< br/> long a [maxsize] = {0 };</P> <p> int N; <br/> printf ("input a number: "); <br/> scanf (" % d ", & N); </P> <p> long T; <br/> T = clock (); </P> <p> int M = factorial (n, a); </P> <p> T = clock ()-T; </P> <p> printf ("% d", a [m]); <br/> for (int l = m-1; L> = 0; l --) // output result <br/>{< br/> printf ("% 04d", a [l]); <br/> If (L * log10_radix) % 10000 = 0) <br/> system ("pause"); <br/>}</P> <p> printf ("/N "); <br/> printf ("time cost: % DMS/N", T); <br/> system ("pause"); </P> <p> return 0; <br/>}</P> <p> void change (long a [], Int & M) <br/>{< br/> int B = 0; // B indicates the carry of the low position to the high position <br/> for (INT I = 0; I <m; I ++) <br/>{< br/> A [I] + = B; <br/> if (a [I] <Radix) // when a [I] <9, it indicates that carry processing is not performed here. // <= <br/> {<br/> B = 0; <br/>}< br/> else if (a [I]> = Radix) // when a [I] is not the highest bit, meet the condition to carry only <br/>{< br/> B = A [I]/Radix; <br/> A [I]-= B * Radix; <br/>}</P> <p> A [I] + = B; <br/> while (A [I]> = Radix) <br/>{< br/> B = A [I]/Radix; <br/> A [I]-= B * Radix; <br/> I ++; <br/> A [I] = B; <br/> ++ m; // Add a node <br/>}</P> <p> // calculate the factorial of N, the result is stored in array A. <br/> int factorial (long N, long a []) <br/>{< br/> memset (A, 0, maxsize * sizeof (long); </P> <p> A [0] = 1; <br/> int m = 0, B = 0; </P> <p> for (INT I = 2; I <= N; I ++) <br/> {<br/> for (Int J = 0; j <= m; j ++) // multiply each element by I, and save it in the original location <br/>{< br/> A [J] = A [J] * I; <br/>}</P> <p> change (a, m); // adjust each of the preceding elements separately, adjust each multiplication time <br/>}</P> <p> return m; <br/>}< br/>
(4)
From http://hi.baidu.com/peng3409/blog/item/87917cd997b747ec38012f6d.html
HDU 1042-calculate the question 10000! About 35000 ~ 36000 bits. The method described in the previous article cannot meet the requirements. Analysis, we can know that the time required for computing is in bit processing. Because we use the 10-digit method, the results lead to a large length of Len. To reduce the computing time, change the input bit to 10 ^ 10, which greatly reduces the carry length. Because 10 ^ 10 exceeds the int range, we use _ int64 (note, two underscores ). The ACM judgment system submitted to Hangzhou University of electronic science and technology shows that it takes 1720 Ms. If you want to further reduce the time, you can extend the base, but the principle is similar.
# Include <iostream> </P> <p> # include <cmath> </P> <p> using namespace STD; </P> <p> const int max_len = 36000; // </P> <p> const _ int64 base5 = 100000; </P> <p> const _ int64 base = base5 * base5; </P> <p >__ int64 X [max_len]; </P> <p> int Len; </P> <p> void fact (int n) </P> <p >{</P> <p> _ int64 carry; </P> <p> int I, J; </P> <p> for (I = 0; I <max_len; ++ I) </P> <p> X [I] = 0; </P> <p> X [0] = 1; </P> <p> Len = 1; </P> <p> for (I = 1; I <= N; ++ I) </P> <p >{</P> <p> C Arry = 0; </P> <p> for (j = 0; j <Len; ++ J) </P> <p >{</P> <p> X [J] = carry + X [J] * I; </P> <p> carry = x [J]/base; </P> <p> X [J] = x [J] % base; </P> <p >}</P> <p> while (carry) </P> <p >{</P> <p> X [J ++] = carry % base; </P> <p> carry = carry/base; </P> <p> Len ++; </P> <p >}</P> <p> int main () </P> <p >{</P> <p> int N; </P> <p> while (scanf ("% d", & N )! = EOF & n> = 0) </P> <p >{</P> <p> fact (n); </P> <p> int I; </P> <p> printf ("% i64d", X [len-1]); </P> <p> for (I = len-2; I> = 0; I --) </P> <p> {// â %x [I] ²» × %9 %» %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</P> <p> If (X [I] <10) printf ("000000000"); </P> <p> else if (X [I] <100) printf ("00000000 "); </P> <p> else if (X [I] <1000) printf ("0000000 "); </P> <p> else if (X [I] <10000) printf ("000000 "); </P> <p> else if (X [I] <100000) printf ("00000 "); </P> <p> else if (X [I] <1000000) printf ("0000 "); </P> <p> else if (X [I] <10000000) printf ("000 "); </P> <p> else if (X [I] <100000000) printf ("00 "); </P> <p> else if (X [I] <1000000000) printf ("0"); </P> <p> printf ("% i64d ", X [I]); </P> <p >}</P> <p> printf ("/N "); </P> <p >}</P> <p> return 0; </P> <p >}< br/>