UVA-10212 the last non-zero digit. factorization prime factor + refresh Theorem

Source: Internet
Author: User

This is a reference to other people's code, and the poj has passed 0 ms. Orz ...... for a sequence after 2, 5 is extracted, for example, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, we can separate the odd and even numbers. For an even sequence, 2, 4, 6, 8, 10 because the original sequence column is extracted from 2, it is degraded to 1, 2, 3, 4, 5, this odd sequence. For the odd sequence 1, 3, 5, 7, 9, we can calculate 3, 5, the number of 7 is equal to the total length n = 10 divided by 10 (unit interval) and then determine whether N % 10 is greater than the required number, for this odd sequence, because 5 is to be extracted, it is changed to 1, and most of them may look better. This recursion will work.

The Code is as follows:

# Include <cstdlib> # include <cstring> # include <cstdio> using namespace STD; int n, m, REC [4] [4] ={{ 6, 2, 4, 8}, {1, 3, 9, 7}, {1, 7, 9, 3}, {1, 9, 1, 9}; int get2 (int x) {If (! X) return 0; return X/2 + get2 (X/2);} int get5 (int x) {If (! X) return 0; return X/5 + get5 (X/5);} int getodd (int x, int base) {If (! X) return 0; return X/10 + (X % 10> = base) + getodd (X/5, base);} // The data is still a complete sequence, this function is used to obtain the int geteven (int x, int base) {If (! X) return 0; return geteven (X/2, base) + getodd (x, base);} int main () {int N2, N3, N5, N7, callback, m2, M3, M5, M7, M9, RET; while (scanf ("% d", & N, & M) = 2) {ret = 1; M = N-m; N2 = get2 (N), M2 = get2 (m); N5 = get5 (N), M5 = get5 (m); N2-= m2, n5-= M5; If (N2> = N5) {N2-= N5, N5 = 0;} else {puts ("5"); continue ;} n3 = geteven (n, 3), M3 = geteven (M, 3); N7 = geteven (n, 7), M7 = geteven (M, 7 ); response = geteven (n, 9), M9 = geteven (M, 9); N3 = (n3-m3) % 4, N7 = (n7-m7) % 4, response = (n9-m9) % 4; If (N2) RET * = rec [0] [n2% 4]; RET * = rec [1] [N3] * rec [2] [N7] * rec [3] [encoding]; printf ("% d \ n ", RET % 10);} return 0 ;}

 

 

My writing method is not the best. 1150 on poj is useless, and the time limit on the ultraviolet A is relatively loose. The code that can pass through poj is basically 0 ms, and the code has been running for more than 7 ms.

See the code for explanations:

# Include <cstdlib> # include <cstring> # include <cstdio> using namespace STD;/* calculates the last non-zero bit of the number of arrays A (m, n, A (m, n) = n! /(N-M )! If M = N-m, the problem is converted to n! /M !, From the perspective of quality factor, we can certainly get n! The quantity of each quality factor must be greater than or equal to M !, If n is small, we can find the numbers of all the quality factors and the quality factors ending with 3, 7, and 9, and then cancel them. Finally, we get the result. If n is large, we only care about the quantity of the qualitative factor 2 and 5, which is equivalent! Write n! = 2 ^ E1 * 5 ^ e2 * X1 * X2 * X3 *..., we can regard the sequence 2 and 5 as 1*2*3*4 *... A sequence of n/2 (n/5) (the original order column is 2*4*6*8*10 *... or 5*10*15*20 *...), for such a re-arranged sequence, we can continue to calculate, we can continue to repeat, and continue, after extracting, we face a series,, so we need to calculate the numbers of them (after non-prime factor decomposition) separately. We can know that the numbers ending with these numbers appear every 10, therefore, we only need to divide the length of the entire sequence by 10 to obtain the result. when rearranging the sequence divided by 2, 5, we should note that we should use the exclusion theorem minus the recomputed */INT n, m, REC [4] [4] = {6, 2, 4, 8}, {1, 3, 9, 7}, {1, 7, 9, 3 }, {1, 9, 1, 9 }}; int get2 (INT x) {If (! X) return 0; return X/2 + get2 (X/2);} int get5 (int x) {If (! X) return 0; return X/5 + get5 (X/5);} int get3 (int x) {If (! X) return 0; return X/10 + (X % 10> = 3) + get3 (X/2) + get3 (X/5) -get3 (X/10);} int get7 (int x) {If (! X) return 0; return X/10 + (X % 10> = 7) + get7 (X/2) + get7 (X/5) -get7 (X/10);} int get9 (int x) {If (! X) return 0; return X/10 + (X % 10> = 9) + get9 (X/2) + get9 (X/5) -get9 (X/10);} int main () {// process to remove all the following zeros, finally, only N2 // N2 indicates the number of factors 2 that contribute the most to the last non-zero bit. // 2, 3, 7. The minimum public multiples of the cycle section are 4 int N2, N3, N5, N7, percentile, M2, M3, M5, M7, M9, RET; while (scanf ("% d", & N, & M) = 2) {ret = 1; M = N-m; N2 = get2 (N ), m2 = get2 (m); N5 = get5 (N), M5 = get5 (m); N2-= m2, N5-= M5; If (N2> = N5) {N2-= N5; N5 = 0;} else {N5-= n2; N2 = 0;} N3 = get3 (N), M3 = get3 (m ); n7 = get7 (N), M7 = get7 (m); latency = get9 (N), M9 = get9 (m); N3-= m3, N7-= M7, convert-= M9; If (N2) {RET * = rec [0] [n2% 4];} If (N5) {RET * = 5;} N3 % = 4, n7 % = 4, records % = 4; ret = RET * rec [1] [N3] * rec [2] [N7] * rec [3] [records]; printf ("% d \ n", RET % 10);} return 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.