Ultraviolet A 10061: How Between Zero's and how between digits?

Source: Internet
Author: User
Tags base 10 logarithm

This question is a bit complicated. The question must be in decimal number n! In hexadecimal notation B, it indicates the number of digits and the total number of digits at the end.

The analysis is as follows:

1. Calculate the number of 0 at the end:

The idea is to set n! The product is decomposed into prime numbers and records the number of prime factor <= B (which is explained in the cause code for less than or equal to B ). Then keep starting from n! Extracted from the factor so that the product can be B, then these factors can make n! A 0 value is generated at the end of B until it cannot be extracted. (Of course, the idea of writing code is to constantly break down B. When n cannot be found! A factor smaller than or equal to B is used to terminate the decomposition of B ). Record the number of times that B is decomposed in the above process, that is, several zeros are generated at the end.

2. calculate the total number of digits:

Since the maximum value represented by M digits in B-base is M-1 decimal format

If the total number of digits is m, then B ^ m-1)-1 <n! <= B ^ m-1, that is, B ^ (S-1) <= n! <B ^ m

Obtain the base 10 logarithm: (m-1) * log10 (B) <= log10 (N !) <M * log10 (B)

Therefore, the method for finding the total number of digits m is obtained.

My problem-solving code is as follows:

# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <cstdlib> using namespace STD; int factor_count [1000]; // records the numbers of int N, B, and INT zeronum () {// decompose n! Memset (factor_count, 0, sizeof (factor_count); For (INT I = 2; I <= N; I ++) {// decompose iint TMP = I; for (Int J = 2; j <= TMP & J <= B; j ++) // only a factor equal to or less than B is required, because the factor greater than B will not become the factor of B {While (TMP % J = 0) {factor_count [J] ++; tmp/= J ;}}} int nzero = 0; while (1) {// decompose bint TMP = B; For (INT I = 2; I <= TMP; I ++) {While (TMP % I = 0 & factor_count [I]> 0) {factor_count [I] --; tmp/= I ;}} if (TMP = 1) nzero ++; else break;} return nzero;} int digitnum (){// Compute logb (N !) Double sum = 0; For (INT I = 2; I <= N; I ++) sum + = log10 (double (I )); sum/= log10 (double (B); // comput digits numreturn floor (sum + 1e-9) + 1;} int main () {While (CIN> N> B) {cout <zeronum () <''<digitnum () <Endl ;}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.