Hdoj Last non-zero Digit in N! [Number Theory]

Source: Internet
Author: User

Find rule!

N! The last Non-zero-digit value. For example, 2 is the last value of 120 instead of 0.
The input N is large and must be saved in large numbers.
Note that the number of the last 0 is equal to the number of five factors. Set f (n) to n! .
Then f (n) = (n % 5 )! * F (n/5) * 2 ^ (n/5) % 10
The number of factor 2 is always greater than 5. From 1 onwards, each of the five consecutive groups are divided into one group. The multiples of five are extracted only after one factor is 5,
Form a new series from 1 to n/5, we have 1*2*3*4*5 = 6*7*8*9*5 = 2 (take the last non-0 bit ), here is 2 ^ (n/5 ).
Just multiply the remaining numbers.
(For example, if n is 123, the first time there will be 121,122,123 three remaining numbers not allocated ).

For example, 23 can be changed to f (23) = (3 )! * F (4) * 2 ^ (4) % 10; f (4) = 4;

So f (23) = 4; reference http://blog.csdn.net/yihuikang/article/details/7721875

 

Last non-zero Digit in N! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 5908 Accepted Submission (s): 1471



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

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

Input to the program is a series of nonnegative integers, each on its own line with no other letters, digits or spaces. for each integer N, you should read the value and compute the last nonzero digit of N !.

Output For each integer input, the program shocould print exactly one line of output containing the single last non-zero digit of N !.

Sample Input
1 2 26 125 3125 9999

Sample Output
124828


# Include
 
  
# Include
  
   
Const int di [4] = {6, 2, 4, 8}; // This is the last iteration of the power of 2; const int pre [10] = {1, 1, 2, 6, 200, 200,}; // the last digit of the top 10; int a [], ls; char s []; void tran (int ls) // convert to place a bit at a [0] {for (int I = ls-1; I> = 0; I --) a [ls-i-1] = s [I]-'0';} void mult () {int I, t = 0; // t is a borrow; for (I = ls-1; i> = 0; I --) {int q = t * 10 + a [I]; a [I] = q/5; t = q % 5 ;} while (ls> 0 & a [ls-1] = 0) -- ls; // exclude the following 0 for careful consideration} int la_no_num () {if (ls = = 1) return pre [a [0]; // if only one bit is output directly or int x1 = pre [a [0] % 5] is returned; // This is f (n % 5) mult (); int x2 = di [(a [0] + a [1] * 10) % 4]; // Why is this 2 ^ (n/5) only the first two digits (Note: same remainder theorem) int ans = (x1 * x2 * la_no_num () % 10; // f (n) = (n % 5 )! * F (n/5) * 2 ^ (n/5) % 10 return ans;} int main () {int la, I; while (~ Scanf ("% s", s) {ls = strlen (s); tran (ls); printf ("% d \ n", la_no_num ());}}
  
 


 

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.