Algorithm problem of 2-channel factorial

Source: Internet
Author: User

To the 51nod new UI to do the problem, by the way, here are 2 factorial questions,

1003 factorial back 0 of the number

How many 0 are behind the factorial of n? The factorial of 6 = 1*2*3*4*5*6 = 720,720 is followed by a 0. Input
A number n (1 <= n <= 10^9)
Number of outputs 0
Input example
Output example
The first reaction to this question is, is this not a regular one?

Well, really, the question is, how does the 0 at the end constitute, think about it, or can think of it, is 5 and the other even multiplied to get, so go on, is not as long as the number of factor 5 is good? Indeed, since the factorial of 1~n, there are even numbers before encountering multiples of 5, as follows:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ..... 5n-1, 5n .... N

Before 5 there were even 2 and 4,10 preceded by even 6 and 8, so that each 5n before an even number can be made with it to form a 0 at the end of the factorial, the 5n extracted, you can get 5^k * k! (k = N/5), so that you can get K 5 first, and then recursively seek k! In 5 of the number, you can find out the number of factor 5, OK, on the code:

#include <iostream>using namespace Std;int main () {    int a, ans = 0;    Cin >> A;    while (a) {        a/= 5;        ans + = A;    }    cout << ans;    return 0;}
I wrote a recursive so that I could get AC.

1008 N's factorial mod p input n and p (p is prime), N! Mod P =? (Mod is modulo%) For example: n = ten, P = 11,10! = 36288003628800% = 10Input
Two number n,p, separated by a space in the middle. (N < 10000, P < 10^9)
Output n! The result of mod p.
Input example
10 11
Output example

See this question, just think factorial can calculate, if n equals 9999, n! is very large, will overflow, with a large number to simulate is also very troublesome, drunk, OK, Internet search, take the mold operation is regular, wherein, there is a law is such,(A * b)% P = (a% p * b% p)% p
I think, how does this rule not see in elementary school ... Well, through this rule, you can launch: N!  % P = ((N-1)! % p) * N p that is, in the case of N-1 factorial, you can find the modulus, and then save the remainder, the next round can be multiplied by the remainder of N, and then to find the modulo, the result of the factorial modulo P of N, the code is as follows:
#include <iostream>using namespace Std;int main () {    long long N, P, ans = 1;    CIN >> N >> P;    For (long long i = 2; I <= N; i++) {        ans = (ans * i)% P;        if (ans = = 0) break;    }    cout << ans;    return 0;}
If you use int here, it can't be passed.

Algorithm problem of 2-channel 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: 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.