<p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 6
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>


class Solution:
    def __init__(self):

      self.a = [0]*(10)
      for i in range(1,10):
        self.a[i] = 10*self.a[i-1]+10**(i-1)
    def countDigitOne(self, n: int) -> int:
      if n == 0:
        return 0
      p = int(round(math.log(n,10),10))
      
      res = 0
      for i in range(p,-1,-1):
        q = n//10**i
        r = n%(10**i)
        res += self.a[i]*(q)
        if q == 1:
          res += r+1
        elif q>1:
          res += 10**i
        n = r
        # print(res, q, r)
      return res

        
16400