Hangzhou Electric Acm1124--factorial

Source: Internet
Author: User

The title means: Ask N! How many zeros are there in the tail.

Just started talking nonsense, in the last paragraph of the problem description to understand the topic to do.

Ask N! The tail of the number of 0, first calculate n! , one more number, it is impossible, and N Max reaches 100000000. And we need to analyze from a mathematical point of view, how is 0 produced?

By writing out the factorial of the previous few numbers, you know that to generate 0, you have to have 5 and an even number to multiply it. What we can know is that in the factorial of a number, the number of 5 is much smaller than the number of even numbers.  And we're going to count the n! The number of the tail of the 0, just to calculate n! The number of 5 can be produced OK.


As an example:

Example: 2015!

2015/5 = 403; that is, 403 is a multiple of 5, plus 403, between 1~2015.

403/5 = 80 (equivalent to 2015/25); Strictly speaking, there are 80 multiples of 25 between 1~2015. 25 is equivalent to 5 * 5, that is, to add 2 * 80, but, think about it, 25 is definitely a multiple of 5, so, the above has been counted once, so only need to add 80 is good.

80/5 = 16 (equivalent to 2015/125), is the 1~2015 between 16 125 multiples, plus 16, for the same reason.

16/5 = 3 (equivalent to 2015/625), is the 1~2015 between 3 625 multiples, plus 3, for the same reason.

3/5 = 0 (equivalent to 2015/3125); not a multiple of 3125.

So 2015! There are 403 + 80 + 16 + 3 consecutive 0 in the back.

The following is the code for the AC:

#include <iostream>using namespace Std;int main () {int T, num;cin >> t;while (t--) {int ans = 0;cin >> num; while (num) {ans + = Num/5;num/= 5;} cout << ans << endl;} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hangzhou Electric Acm1124--factorial

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.