Factorial Analysis poj1150 3406 zoj1222 2358

Source: Internet
Author: User

There are several types of factorial problems:

1. Calculate the number of 0 at the end of the factorial, divide it by 5, and accumulate.

2. How many digits are there in the result of factorial? stirling formula: n! ≈ Sqrt (2 * PI * n) * (n/e) ^ n, takes the base-10 logarithm directly, and the integer part is the number of digits. 3. Calculate the last non-zero bit of the factorial. This type of problem is complicated. We will focus on this issue in the topic.

 

First, check POJ1150.

Calculate the last non-zero position of m of n.

Question Analysis: m of n is P (n, m) = n! /(N-m )!, So this is the last non-zero bit of the two factorial. We usually process the factorial one by one. First, let's take a look at the general factorial of a number n. How do we deal with non-zero digits at the end of it.

The factor of 10 is 2 and 5. These two numbers do not belong to the contraction system of the modulo 10. Therefore, we need to remove these two factors when processing the number, remove the 5 and 2 factors from all numbers.

First, we use f (n, x) to represent the number of The number of x at the end of the number less than or equal to n. N can be decomposed into 1, 3, 5, 7, 9... 2, 4, 6, 8, 10...

Because we have removed the two factors from the even number, we have actually changed them to, 9...

So we can obtain the recursive formula f (n, x) = f (n/2, x) + g (n, x), g (n, x) it indicates the number of The number of tails x in an odd number less than or equal to n.

Then how can we find g (n, x)? First, n/10 must be a part of it. Second, we need to see whether n> = x is true. If it is true, it will add 1. If it is not true, it will not. Last note, we still have the number of 5 factors in the odd number, so we need to remove the 5 factor, such as 5 15 25 35 45... after removing 5, it becomes 1 3 5 7 9 ..., therefore, g (n, x) = n/10 + (n % 10> = x) + g (n/5, x ).

With these two Recursive Formulas plus the recursive division for finding the numbers of 5 and 2 factors, we will soon be able to get the number of 2 factors, the number of 5 factors, number of 3 7 9 factors at the end. Below we will list a circular table mod2 [4] = {6, 2, 4, 8}, mod3 [4] = {1, 3, 9, 7}, mod7 [4] = {, 9, 3 }, mod9 [4] = {,} indicates the cycle section of the ending number of each factor multiplied by itself. Note that due to the contraction of 2 Non-modulo 10, SO 2 ^ 0 = 1, so pay special attention to this, so only its cyclic section does not start from 0, 0 should be processed separately. Because the factorial of n-m must be a subset of the factorial of n, We can subtract the number of factors directly.

[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace std;
Int cnt [8];
Int mod2 [4] = {6, 2, 4, 8 };
Int mod3 [4] = {1, 3, 9, 7 };
Int mod7 [4] = {1, 7, 9, 3 };
Int mod9 [4] = };
Int getpow (int n, int k)
{
Int sum = 0;
While (n)
{
Sum + = n/k;
N/= k;
}
Return sum;
}
Int g (int n, int k)
{
If (n = 0) return 0;
Return n/10 + (n % 10> = k) + g (n/5, k );
}
Int get (int n, int k)
{
If (n = 0) return 0;
Return get (n/2, k) + g (n, k );
}
Int main ()
{
Int n, m;
While (~ Scanf ("% d", & n, & m ))
{
Int res = 1;
Memset (cnt, 0, sizeof (cnt ));
Cnt [2] = getpow (n, 2)-getpow (n-m, 2 );
Cnt [5] = getpow (n, 5)-getpow (n-m, 5 );
Cnt [3] = get (n, 3)-get (n-m, 3 );
Cnt [7] = get (n, 7)-get (n-m, 7 );
Cnt [9] = get (n, 9)-get (n-m, 9 );
If (cnt [2]> cnt [5]) {res * = mod2 [(cnt [2]-cnt [5]) % 4];}
Else if (cnt [2] <cnt [5]) {res * = 5 ;}
Res = res * mod3 [cnt [3] % 4] * mod7 [cnt [7] % 4] * mod9 [cnt [9] % 4];
Printf ("% d \ n", res % 10 );
}
Return 0;
}

# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace std;
Int cnt [8];
Int mod2 [4] = {6, 2, 4, 8 };
Int mod3 [4] = {1, 3, 9, 7 };
Int mod7 [4] = {1, 7, 9, 3 };
Int mod9 [4] = };
Int getpow (int n, int k)
{
Int sum = 0;
While (n)
{
Sum + = n/k;
N/= k;
}
Return sum;
}
Int g (int n, int k)
{
If (n = 0) return 0;
Return n/10 + (n % 10> = k) + g (n/5, k );
}
Int get (int n, int k)
{
If (n = 0) return 0;
Return get (n/2, k) + g (n, k );
}
Int main ()
{
Int n, m;
While (~ Scanf ("% d", & n, & m ))
{
Int res = 1;
Memset (cnt, 0, sizeof (cnt ));
Cnt [2] = getpow (n, 2)-getpow (n-m, 2 );
Cnt [5] = getpow (n, 5)-getpow (n-m, 5 );
Cnt [3] = get (n, 3)-get (n-m, 3 );
Cnt [7] = get (n, 7)-get (n-m, 7 );
Cnt [9] = get (n, 9)-get (n-m, 9 );
If (cnt [2]> cnt [5]) {res * = mod2 [(cnt [2]-cnt [5]) % 4];}
Else if (cnt [2] <cnt [5]) {res * = 5 ;}
Res = res * mod3 [cnt [3] % 4] * mod7 [cnt [7] % 4] * mod9 [cnt [9] % 4];
Printf ("% d \ n", res % 10 );
}
Return 0;
}

 

 

Let's take a look at POJ3406.

N! /(M! * (N-m )!) The last non-zero bit

Like the above question, but because the product of the factorial of m and the factorial of n-m is not a subset of the factorial of n, we can observe and find that, only the number of 3 in the factorial of n is less than the sum of the factorial of m and the factorial of n-m, in this case, we can break down the number of 9 in the n factorial. Another way is to add a loop model (I don't understand it)

[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace std;
Int cnt [8];
Int mod2 [4] = {6, 2, 4, 8 };
Int mod3 [4] = {1, 3, 9, 7 };
Int mod7 [4] = {1, 7, 9, 3 };
Int mod9 [4] = };
Int getpow (int n, int k)
{
Int sum = 0;
While (n)
{
Sum + = n/k;
N/= k;
}
Return sum;
}
Int g (int n, int k)
{
If (n = 0) return 0;
Return n/10 + (n % 10> = k) + g (n/5, k );
}
Int get (int n, int k)
{
If (n = 0) return 0;
Return get (n/2, k) + g (n, k );
}
Int main ()
{
Int n, m;
While (~ Scanf ("% d", & n, & m ))
{
Int res = 1;
Memset (cnt, 0, sizeof (cnt ));
Cnt [2] = getpow (n, 2)-getpow (n-m, 2)-getpow (m, 2 );
Cnt [5] = getpow (n, 5)-getpow (n-m, 5)-getpow (m, 5 );
Cnt [3] = get (n, 3)-get (n-m, 3)-get (m, 3 );
Cnt [7] = get (n, 7)-get (n-m, 7)-get (m, 7 );
Cnt [9] = get (n, 9)-get (n-m, 9)-get (m, 9 );
Cnt [3] + = cnt [9] * 2; cnt [9] = 0;
If (cnt [2]> cnt [5]) {res * = mod2 [(cnt [2]-cnt [5]) % 4];}
Else if (cnt [2] <cnt [5]) {res * = 5 ;}
Res = res * mod3 [cnt [3] % 4] * mod7 [cnt [7] % 4] * mod9 [cnt [9] % 4];
Printf ("% d \ n", res % 10 );
}
Return 0;
}

# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace std;
Int cnt [8];
Int mod2 [4] = {6, 2, 4, 8 };
Int mod3 [4] = {1, 3, 9, 7 };
Int mod7 [4] = {1, 7, 9, 3 };
Int mod9 [4] = };
Int getpow (int n, int k)
{
Int sum = 0;
While (n)
{
Sum + = n/k;
N/= k;
}
Return sum;
}
Int g (int n, int k)
{
If (n = 0) return 0;
Return n/10 + (n % 10> = k) + g (n/5, k );
}
Int get (int n, int k)
{
If (n = 0) return 0;
Return get (n/2, k) + g (n, k );
}
Int main ()
{
Int n, m;
While (~ Scanf ("% d", & n, & m ))
{
Int res = 1;
Memset (cnt, 0, sizeof (cnt ));
Cnt [2] = getpow (n, 2)-getpow (n-m, 2)-getpow (m, 2 );
Cnt [5] = getpow (n, 5)-getpow (n-m, 5)-getpow (m, 5 );
Cnt [3] = get (n, 3)-get (n-m, 3)-get (m, 3 );
Cnt [7] = get (n, 7)-get (n-m, 7)-get (m, 7 );
Cnt [9] = get (n, 9)-get (n-m, 9)-get (m, 9 );
Cnt [3] + = cnt [9] * 2; cnt [9] = 0;
If (cnt [2]> cnt [5]) {res * = mod2 [(cnt [2]-cnt [5]) % 4];}
Else if (cnt [2] <cnt [5]) {res * = 5 ;}
Res = res * mod3 [cnt [3] % 4] * mod7 [cnt [7] % 4] * mod9 [cnt [9] % 4];
Printf ("% d \ n", res % 10 );
}
Return 0;
}

 


Finally, let's take a look at the BT question ZOJ1222.

This question is available in many OJ, but it is only about an order of magnitude small and can be easily ac using the above method. However, it is said that the input is a string and may contain more than 100 digits. Therefore, in the face of such high-precision large numbers, it should be solved in a new way.

The idea remains unchanged. We still take 2 and 5 as the core, and multiply the ending number of each number. It is just because of the rapid expansion of the number that we cannot use the original recursive solution, A loop section is used: int mod [10] = {,}. This indicates that the number is multiplied from 0 to 9 (5 is changed to 1) is not 0. When n is less than 5, mod [n] is directly output. to remove the ending number 0, we need to remove 2 and 5 of the same number. We found that the number of 2 is far more than 5, and the number of factor 2 is the most, therefore, the final result must be an even number, so there is such a special ending number Division: 2/2 = 6, 4/2 =/2 = 8/2 = 4. At the same time, when dividing by the number of 2, there is still a division cycle due to the large number of 2, and the length of the cycle is 4. and the 10 tails are multiplied by 6. When n> 10, we can get a formula:


Now we can use this formula to solve the problem. It is worth noting that n is a very large number. When dividing by 5, we can use high-precision division.

[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace std;
Char str [1001];
Int mod [10] = {, 4 };
Int a [1001];
Int getAns (int cnt)
{
If (cnt = 1 & a [0] <5) return mod [a [0];
Int res = 0;
Int w = a [0];
For (int I = cnt-1; I> = 0; -- I)
{
Res = res * 10 + a [I];
A [I] = res/5;
Res = res % 5;
}
If (a [cnt-1] = 0) -- cnt;
Int m = (a [1] * 10 + a [0]) % 4;
Int tmp = getAns (cnt) * mod [w] * 6% 10;
While (m --)
{
If (tmp = 2) tmp = 6;
Else if (tmp = 6) tmp = 8;
Else tmp/= 2;
}
Return tmp;
 
}
Int main ()
{
While (~ Scanf ("% s", str ))
{
Int len = strlen (str );
Memset (a, 0, sizeof ());
For (int I = 0; I <len; ++ I)
{
A [I] = str [len-1-i]-'0 ';
}
Int ans = getAns (len );
Printf ("% d \ n", ans );
}
Return 0;
}

# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace std;
Char str [1001];
Int mod [10] = {, 4 };
Int a [1001];
Int getAns (int cnt)
{
If (cnt = 1 & a [0] <5) return mod [a [0];
Int res = 0;
Int w = a [0];
For (int I = cnt-1; I> = 0; -- I)
{
Res = res * 10 + a [I];
A [I] = res/5;
Res = res % 5;
}
If (a [cnt-1] = 0) -- cnt;
Int m = (a [1] * 10 + a [0]) % 4;
Int tmp = getAns (cnt) * mod [w] * 6% 10;
While (m --)
{
If (tmp = 2) tmp = 6;
Else if (tmp = 6) tmp = 8;
Else tmp/= 2;
}
Return tmp;

}
Int main ()
{
While (~ Scanf ("% s", str ))
{
Int len = strlen (str );
Memset (a, 0, sizeof ());
For (int I = 0; I <len; ++ I)
{
A [I] = str [len-1-i]-'0 ';
}
Int ans = getAns (len );
Printf ("% d \ n", ans );
}
Return 0;
}
 

4. Can the number n be expressed as the sum of several different factorial values?

In fact, this problem is irrelevant to number theory and is a search problem.
Because n is relatively small, it can only be 0 !~ 9! So you can use dfs to search directly.

[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <map>
Using namespace std;
Int a [10] = {24,120,720,504, 362880 };
Int sum;
Bool vis [11];
Map <int, int> mymap;
Void dfs (int n)
{
If (n = 10)
{
If (mymap. find (sum) = mymap. end () mymap [sum] = 1;
Return;
}
Sum + = a [n];
Dfs (n + 1 );
Sum-= a [n];
Dfs (n + 1 );
}
Int main ()
{
Memset (vis, false, sizeof (vis ));
Sum = 0;
Mymap. clear ();
Dfs (0 );
Int n;
While (~ Scanf ("% d", & n ))
{
If (n <0) break;
If (n = 0 | mymap. find (n) = mymap. end () puts ("NO ");
Else puts ("YES ");
}
Return 0;
}

# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <map>
Using namespace std;
Int a [10] = {24,120,720,504, 362880 };
Int sum;
Bool vis [11];
Map <int, int> mymap;
Void dfs (int n)
{
If (n = 10)
{
If (mymap. find (sum) = mymap. end () mymap [sum] = 1;
Return;
}
Sum + = a [n];
Dfs (n + 1 );
Sum-= a [n];
Dfs (n + 1 );
}
Int main ()
{
Memset (vis, false, sizeof (vis ));
Sum = 0;
Mymap. clear ();
Dfs (0 );
Int n;
While (~ Scanf ("% d", & n ))
{
If (n <0) break;
If (n = 0 | mymap. find (n) = mymap. end () puts ("NO ");
Else puts ("YES ");
}
Return 0;
}

 


 

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.