block-quote On this pagechevron-down
copy Copy chevron-down
Week 3 Math / Number Theory chevron-right Week 3 Math / Number Theory 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 ⌈ x ⌉ is the ceiling function, which rounds a number up to the closest larger integer.
Another important property of logs: log ( x y ) = log ( x ) + log ( y ) \log(xy) = \log(x) + \log(y) log ( x y ) = log ( x ) + log ( y ) .
n ! n! n ! is only a power of 10 if n = 0 n=0 n = 0 or n = 1 n=1 n = 1 , so handle those cases separately. Otherwise:
⌈ log 10 n ! ⌉ = ⌈ log 10 [ ( n ) ( n − 1 ) … ( 1 ) ] ⌉ = ⌈ log 10 ( n ) + ⋯ + log 10 ( 1 ) ⌉ \lceil \log_{10}n! \rceil = \lceil \log_{10} [(n)(n-1)\dots(1)] \rceil = \lceil \log_{10}(n)+\dots+\log_{10}(1)\rceil ⌈ log 10 n !⌉ = ⌈ log 10 [( n ) ( n − 1 ) … ( 1 )]⌉ = ⌈ log 10 ( n ) + ⋯ + log 10 ( 1 )⌉ Store the sums log 10 ( 1 ) + ⋯ + log 10 ( k ) \log_{10}(1) + \dots + \log{10}(k) log 10 ( 1 ) + ⋯ + 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.