LogoLogo
  • CP Gymnasium
  • Week 3 Math / Number Theory
    • Week 3 Math / Number Theory
      • Problem 1: Tetration
      • Problem 2: Peragrams
      • Problem 3: Veci
      • Problem 4: Architecture
      • Problem 5: Joint Attack
      • Problem 6: How Many Digits?
      • Problem 7: Abstract Painting
  • Week 4 Array / Greedy
    • Week 4 Array / Greedy
      • Problem 1: Vaccine Efficacy
      • Problem 2: Frosh Week
      • Problem 3: Inquiry
      • Problem 4: Bank Queue
      • Problem 5: Log Land
  • Week 6 Sorting / Binary Search
    • Week 6 Sorting / Binary Search
      • Problem 1: Falling Apart
      • Problem 2: Synchronizing Lists
      • Problem 3: Distributing Ballot Boxes
      • Problem 4: Financial Planning
      • Problem 5: Big Boxes
  • Week 7 Dynamic Programming
    • Week 7 Dynamic Programming
      • Problem 1: Ocean's Anti-11
      • Problem 2: Batmanacci
      • Problem 3: Radio Commercials
      • Problem 4: Welcome to Code Jam (Hard)
      • Problem 5: Honeycomb Walk
  • Week 8 Graph Traversals
    • Week 8 Graph Traversals
      • Problem 1: Reachable Roads
      • Problem 2: Money Matters
      • Problem 3: Squawk Virus
      • Problem 4: Beehives
      • Problem 5: Running MoM
      • Problem 6: Amanda Lounges
Powered by GitBook
On this page
  1. Week 3 Math / Number Theory
  2. 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:

\text{# of digits in }x = \begin{cases} \log_{10} (x)+1 & : x \text{ is a power of }10 \\ \lceil \log_{10} (x) \rceil & : \text{ otherwise} \end{cases}

⌈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⁡(xy)=log⁡(x)+log⁡(y)\log(xy) = \log(x) + \log(y)log(xy)=log(x)+log(y).

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

⌈log⁡10n!⌉=⌈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⌈log10​n!⌉=⌈log10​[(n)(n−1)…(1)]⌉=⌈log10​(n)+⋯+log10​(1)⌉

Store the sums log⁡10(1)+⋯+log⁡10(k) \log_{10}(1) + \dots + \log{10}(k)log10​(1)+⋯+log10(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.

PreviousProblem 5: Joint AttackNextProblem 7: Abstract Painting

Last updated 4 years ago