Problem 6: How Many Digits?

https://open.kattis.com/problems/howmanydigits

The number of digits of an integer can be calculated by the following formula:

x\lceil x \rceil is the ceiling function, which rounds a number up to the closest larger integer.

Another important property of logs: log(xy)=log(x)+log(y)\log(xy) = \log(x) + \log(y).

n!n!is only a power of 10 if n=0n=0 or n=1n=1, so handle those cases separately. Otherwise:

log10n!=log10[(n)(n1)(1)]=log10(n)++log10(1)\lceil \log_{10}n! \rceil = \lceil \log_{10} [(n)(n-1)\dots(1)] \rceil = \lceil \log_{10}(n)+\dots+\log_{10}(1)\rceil

Store the sums log10(1)++log10(k) \log_{10}(1) + \dots + \log{10}(k)in a vector, and output the ceiling of that sum for each test case.

C++ has functions ceil(x) and log10(x) that can be used for calculations. Be careful that ceil() returns a double, so cast to int or long long before printing.

Last updated