How many times zero's and how many digits?

Source: Internet
Author: User

How many zeros and how many digits?

Input: standard input

Output: standard output

 

Given a decimal integer number you will have to find out how many trailing zeros will be there in its factorial in a given number system and also you will have to find how many digits will its factorial have in a given number system? You can assume that for a bbased number system there are B different symbols to denote values ranging from 0... B-1.

 

Input

There will be several lines of input. each line makes a block. each line will contain a decimal number N (a 20bit unsigned number) and a decimal number B (1 <B <= 800 ), which is the base of the number system you have to consider. as for example 5! = 120 (in decimal) but it is 78 in hexadecimal number system. So in Hexadecimal 5! Has no trailing zeros

 

Output

For each line of input output in a single line how does trailing zeros will the factorial of that number have in the given number system and also how does digits will the factorial of that number have in that given number system. separate these two numbers with a single space. you can be sure that the number of trailing zeros or the number of digits will not be greater than 2 ^ 31-1

 

Sample Input:

2 10
5 16
5 10


 

Sample Output:

0 1
0 2
1 3

N! The number of digits of the bas-base m and the number of digits after 0.

Solution: 1. Number of digits: when the base is 10, 10 ^ m-1) <n <10 ^ m, and the two sides go with log10, m-1 <log10 (n) the number of digits of <m, n is (m-1 ).

PS: <1> log10 (a * B) = log10 (a) + log10 (B) evaluate n! .

<2> logb (a) = log c (a)/log c (B) converts the hexadecimal digits.

<3> for the accuracy of floating point numbers, the log function is used to calculate the number of digits. The calculation accuracy of log functions is incorrect. So the last step is to add a 1e-9 and then floor.

2, set n! It is decomposed into quality factors and stored in the array. It is decomposed multiple times for bas until the elements in the array are less than 0.

 

#include<stdio.h>#include<string.h>#include<math.h>#define N 10000int num[N];int count_digit(int n, int bas){double sum = 0;for (int i = 1; i <= n; i++)sum += log10(i);sum = sum / log10(bas);return floor(sum + 1e-9) + 1;}int count_zore(int n, int bas){memset(num, 0, sizeof(num));for (int i = 2; i <= n; i++){int g = i;for (int j = 2; j <= g && j <= bas; j++){while (g % j == 0){num[j]++;g = g / j;}}}int cnt = 0;while (1){int g = bas;for (int j = 2; j <= bas; j++){while (g % j == 0){if (num[j] > 0)num[j]--;elsegoto out;g = g / j;}}cnt++;}out:return cnt;}int main(){int n, bas;while (scanf("%d%d", &n, &bas) != EOF){int ndigit = count_digit(n, bas);int nzore = count_zore(n, bas);printf("%d %d\n", nzore, ndigit);}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.