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 larger than the limit value 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. Each of the five consecutive groups is divided into one group starting from 1. The multiples of five are only extracted after a factor of five,
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 numbers left unallocated ).

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

So F (23) = 4; lead test 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 descriptionthe 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.
 
Inputinput 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 !.

Outputfor 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 <stdio. h> # include <string. h> const int di [4] = {6, 2, 4, 8}; // This is the last loop of the power of 2; const int pre [10] = {1, 1, 2, 6, 200,}; // the last digit of the top 10; int A [], ls; char s [200]; 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 } Int la_no_num () {If (LS = 1) return pre [A [0]; // assume there is only one direct output or Int X1 = pre [A [0] % 5]; // 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 ());}}


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.