HDU 1066 last non-zero digit in n!

Source: Internet
Author: User

I am confused ............

Post:

Http://blog.sina.com.cn/s/blog_59e67e2c0100a7yx.html

 

First, reference the leemars report:

N is required for this question! The last non-zero number of, if the general practice, first count the number of 2 and 5, then
Then multiply by 2 to get the TLE. Therefore, we need to simplify the process:

In order to remove 0, we put forward all the factors 2 and 5 and put them to the end for further processing. N! N
The number can be divided into two heaps: an odd number and an even number. You can write (2 ^ m) * (M!) by multiplying an even number !), M = N Div 2. M! Yes
Recursive processing, so now we only need to discuss the multiplication of odd numbers. Consider 1*3*5*7*9*11*13*15*17 *... * n (if
N is an even number is a N-1), here is the multiples of 5 are 5, 15, 25, 35,..., which can be 5 proposed
, To (5 ^ p) * (1*3*5*7*9 *...), P is enclosed in brackets, P = (N Div 5 + 1) Div 2, and then
The brackets can be further raised to 5 for Recursive processing. The remaining number is 1*3*7*9*11*13.
* 17*19 *.... We only need their single digits because (1*3*9*11*13
*...) Mod 10 = (1*3*7*9*1*3 *...) mod 10. We list the remainder table,
1 3 1 9 9 7 9 1 1 3 1 9 9 7 9 ....... We found that every eight mod 10 results in a loop.
After calculating the odd number of results, let's look back at the number of 2 and 5 to be multiplied. Pairing 2 and 5 is all N.
! The value 0 is followed by the number 2. If the remaining two exist, consider that the single digit of 2 ^ N is 2 4 8 6 2 4
8 6 ...... Every four items have a loop. After finding this single digit, multiply it with the previous result.
Answer. Because we fully process the two and five factors separately, in all multiplication, we only need to calculate the single-digit multiplication and only keep the single-digit result.

But what surprised me is: Why do I always submit wa? I learned later that the reason is that N is quite large.
! Reached 10 ^ 100! Use a large number for processing! The four compilation Switches of GPC are completely off. Naturally, they cannot be found! And
The aboveAlgorithmChanging to a large number will be troublesome. Is there any other good method?

The answer is yes. Let's set F (n) to n! .

First, consider simple. Consider a certain n! (N <10), we first propose the multiples of all 5, and use 1 to replace the original
The position in multiples of 5. Because the multiples of 5 are all removed, so there will be no ending number 0. First
The ending number of the factorial of 0 .. 9 is listed (note that the position of the multiples of 5 is 1). You can get table [0 .. 9] =
(1, 1, 2, 6, 4, 4, 4, 8, 4, 6 ). For n <5, directly output table [N]. For N>
= 5. Because a 5 is proposed, a 2 is required to be matched with 10, that is, dividing the ending number by 2. Note that except 0
! And 1 !, The last non-zero number of a factorial must be an even number, so there is a special division rule: 2/2
= 6, 4/2 = 2, 6/2 = 8, 8/2 = 4. 2/2 = 12/2 = 6,
6/2 = 16/2 = 8. In this way, we can get the following formula:
Code:

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

Consider complicated ones. Consider a certain n! (N> = 10), we first propose the multiples of all 5, and use 1 to replace the original
In multiples of 5. Because the multiples of 5 are all removed, so there will be no ending number 0. View
Check the ending number of the product of the remaining number. Through the table, we find that the ending number of the product of the 10 numbers is 6,
The ending number of 6*6 is 6, so we divide the remaining number into a group every 10, then the ending number of the product of the remaining number
It is only related to the last group, that is, the last digit of N. Because we have raised the multiples of 5
, N! In one request, we can propose a multiple of [N/5] to five. If there is a number of five, we need to assign the number of two to ten.
Divide the number of five by the number of two. Note that the result of Division 2 changes to four loops, so if
There is a 5, you only need to divide (a mod 4) Twice 2. A mod 4 is only related to the last two digits of.
. The number of the remaining 5 is [N/5] Because all 5 has been processed. So we can get
A recursive relationship:
Code:

 F ([N/5]) * Table [ending number of N] * 6
F (n) = ----------------------------------- (n> 10)
 2 ^ ([N/5] mod 4)

In this way, we get an O (log5 (N) algorithm. The Division of 5 can be done with a high-precision addition. Multiply by 2 and then divide 10.
Yes. The entire algorithm is quite clever and easy to write.

Because 2 ^ N is a 4-loop section

In addition, table [N] uses 10 as the cyclic section.

So starting from 10

    F ([N/5]) * Table [ending number of N] * 6
F (n) = ----------------------------------- (n> 10)
 2 ^ ([N/5] mod 4)

In addition to f [N/5], the formula on the right is circular section 20.

Write the number mod [20] = {,} at the end of the loop}

 

Overall Solution

 

View 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 [[ 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 ;
}

 

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.