HDU 1066 Last Non-zero Digit in N! (Number theory--n! The last non-0 digit in the

Source: Internet
Author: User

Last Non-zero Digit in N!
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 6432 Accepted Submission (s): 1593


Problem DescriptionThe Expression n!, read as "n factorial," denotes the product of the first N positive integers, where n is nonnegative. So, for example,
n N!
0 1
1 1
2 2
3 6
4 24
5 120
10 3628800

For this problem, you is to write a program, which can compute the last non-zero digit of the factorial for N. For example, if your program was asked to compute the last nonzero digit of 5!, your program should produce "2" because 5! = 120, and 2 are the last nonzero digit.

Inputinput to the program are a series of nonnegative integers, each on their own line with no other letters, digits or space S. For each integer N, you should read the value and compute the last nonzero digit of n!.

Outputfor each integer input, the program should print exactly one line of output containing the single last Non-zero digi T of n!.

Sample Input
1 2 26 125 3125 9999

Sample Output
124828

Sourcesouth Central USA 1997



Test instructions: Ask N! The last non-0 digit.


Analytical:

This problem requires the last non-0 number of n!, if the general practice, first statistics 2 and 5 of the number, and then
After 2, the resulting will be tle. So we need to make it simpler:

In order to get rid of 0, we put all the factors 2 and 5 are put forward, put in the final processing. n multiplied by the n! in a
The number can be divided into two piles: odd and even. Even multiplication can be written as (2^m) * (m!), M=n DIV 2. M! can
Recursive processing, so now it's just about odd multiplication. Consider 1*3*5*7*9*11*13*15*17* ... *n (if
n is an even number is N-1), which is a multiple of 5 has 5,15,25,35, ..., which can be raised in 5
, becomes (5^p) * (1*3*5*7*9* ...), followed by brackets P-entries, p= (N div 5+1) DIV 2, and then
The parentheses of the face can continue to be raised 5, recursive processing. Now the remaining number is 1 * 3 * 7 * 9 * 11 * 13
* 17 * 19 * .... These numbers we only need their single digit, because (1 * 3 * 9 * 11 * 13
* ... ) MOD 10 = (1 * 3 * 7 * 9 * 1 * 3 * ...) MOD 10. We list the remaining tables,
131997911319979....... We found that each of the eight mod 10 results was a loop.
After counting the surprising number of results, we look back at how many 2 and 5 need to be multiplied. Pairing 2 and 5 is all n.
The back of the 0, look at the remaining 2 there are several. If there is the remaining 2, consider the single digit of 2^n 2 4 8 6 2 4
8 6 ... Every four items a loop, find out this single digit, and the previous results multiplied, then take the single digit is
Answer. Since we are completely dealing with the 2 and 5 factors, we only need to calculate single-digit multiplication in all multiplication, and only the single-digit result is preserved.

But what amazes me is: why do I submit always wa? Later I learned that the reason is that the problem of N is quite large
! Reached the 10^100! It's going to be handled in large numbers! GPC Four compiler switch all off, nature can't find out! And
The above algorithm will be cumbersome after it is replaced by a large number. Is there any other good way?

The answer is yes. We set F (N) to denote the mantissa of the n!.

Consider the simple first. Consider a certain n! (N < 10), we first put all multiples of 5, using 1 instead of the original
The position of multiples of 5. Since multiples of 5 are all taken away, so there is no mantissa 0. Let's first put
0..9 the number of factorial tails (note that the position of multiples of 5 is 1), you can get table[0..9] =
(1, 1, 2, 6, 4, 4, 4, 8, 4, 6). For n < 5, direct output TABLE[N] can be; for n >
= 5, because a 5 is proposed, so a 2 is required to match 10, and the mantissa is divided by 2. Notice that in addition to 0
! and 1!, the last non-0 number of factorial must be even, so there is a very special Division law: 2/2
= 6,4/2 = 2,6/2 = 8,8/2 = 4. The Special One is 2/2 = 12/2 = 6,
6/2 = 16/2 = 8. So we can get the following formula:
Code:

Table[n]
F (n) =------------(0 <= N < 10)
2^ ([N/5])

Again, consider the complex. Consider a certain n! (N >= 10), we first put all the multiples of 5, using 1 instead of the original
To the position of multiples of 5. Since multiples of 5 are all taken away, so there is no mantissa 0. Our view
To examine the mantissa of the product of the remainder, through table tables, we find that the mantissa of the product of the 10 numbers is 6,
The mantissa of 6 * 6 is still 6, so we divide the remaining number by one group per 10, then the mantissa of the product of the remaining number
Relates only to the last set of cases, that is, the last digit of N. Because we brought in multiples of 5.
, n! in one can be raised [N/5] a multiple of 5, how many 5, you need to have how many 2 with it 10, the
How many 5 will be divided by how many 2. Notice that the result of the change in addition to 2 is 4 cycles, so if
There is a 5, only need to except (a MOD 4) times 2 on it. A MOD 4 is only related to the last two digits of a, very good.
Count The remaining multiples of 5, since 5 have been completely disposed of, become [n/5]!]. So, we can get
A recursive relationship:
Code:

F ([N/5]) * Table[n of the mantissa] * 6
F (n) =-----------------------------------(n > 10)
2^ ([N/5] MOD 4)

So we get an O (log5 (N)) algorithm, divisible by 5 can be done with high-precision addition, multiply 2 and divide 10 namely
Can The whole algorithm is quite ingenious and easy to write.

Because the 2^n is a cyclic section with 4
and Table[n] is a circular section with 10.
So from 10 onwards
F ([N/5]) * Table[n of the mantissa] * 6
F (n) =-----------------------------------(n > 10)
2^ ([N/5] MOD 4)
The right side of the equation except F[N/5] is a circular section with 20
Write the end number of the Loop mod[20]={1,1,2,6,4,2,2,4,2,8,4,4,8,4,6,8,8,6,8,2}

This is the whole idea.


Note: The above analysis for reference from: Anonymous blog




AC Code:

#include <stdio.h> #include <string.h>int mod[20]={1,1,2,6,4,2,2,4,2,8,4,4,8,4,6,8,8,6,8,2};char n[1000 ];int A[1000];int Main () {int i,c,t,len;    while (scanf ("%s", N)!=eof) {  t=1;  Len=strlen (n);   for (i=0;i<len;i++)   a[i]=n[len-1-i]-' 0 ';  while (len)  {   len-=!a[len-1];   t=t*mod[a[1]%2*10+a[0]]%10;     for (c=0,i=len-1;i>=0;i--)          c=c*10+a[i],a[i]=c/5,c%=5;  }  printf ("%d\n", t); } return 0;}






HDU 1066 Last Non-zero Digit in N! (Number theory--n! The last non-0 digit in the

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.