Nyoj84 factorial 0

Source: Internet
Author: User
0 time limit of factorial: 3000 MS | memory limit: 65535 kb difficulty: 3
Description
Calculate n! In decimal format.
Input
Enter an integer N in the first line to indicate the number of groups of test data (1 <= n <= 100)
Each group of test data occupies one row, with only one integer m (0 <= m <= 10000000)
Output
Number of the last 0 in the decimal number of the factorial output M
For example, 5! = 120 then the number of the last 0 is 1
Sample Input
63601001024234568735373
Sample output
0142425358612183837

Solution: in prime numbers, a "0" is displayed at the end only when 2 and 5 are multiplied, so as long as M is decomposed into prime factors, and then the number of 2 and 5 is counted, the smaller one is the answer.
Further, after M decomposition, the number of 2 is definitely more than 5,
Then the problem is further simplified, as long as the number of five of all prime factors is counted.

For example:

1 --> multiples of 5 in 100

There are 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100.

20 5

However, after dividing by 5, there are still 5

5 10 15 20

At this time, there are 4 5

That is to say, 1 --> 100 has a total of 24 5; that is, the number of 0.

The Code uses recursion to calculate the number of 5:

# Include <stdio. h> int COUNT = 0; // count int factorial (INT m) {If (M = 0) return count; else {count + = m/5; return factorial (M/5) ;}} int main () {int m; int N; scanf ("% d", & N); While (n --) {COUNT = 0; scanf ("% d", & M); printf ("% d \ n", factorial (m )); // recursive operation Implementation} return 0 ;}


Nyoj84 factorial 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.