LIBJOB - Editorial

PROBLEM LINK:

Practice
Contest

Author: Riddhish Lichade
Editorialist: Riddhish Lichade

DIFFICULTY:

EASY

PREREQUISITES:

Maths, Loop

PROBLEM:

It’s the summer vacation in DBATU. Meena decided to get a part-time job in the vacations to support his living. Finally, he found a job at the local library, which has opened recently.

There are N books in the library. Meena was assigned a job to label all the books serial wise from 1 to N. Meena approached the to the only printing press in DBATU. The printing press had a policy of printing each digit separately on a separate sticker. For example, for a number say ‘12’ the press will give 2 stickers with numbers ‘1’ and ‘2’ in place of a single sticker with ‘12’. The cost of printing of every sticker was Rs. 2

Meena had no choice but to have the stickers printed differently as it was the only press available.

Find the total amount of money needed to print the stickers.

EXPLANATION:

We have to simply count the number of digits in all the natural numbers till N. That will be the number of stickers and then multiply it by 2, the cost of a single sticker.
To calculate the number of stickers(say X) we can derive a formula:
for N \leq 9: X = N = 1 * (N+1) - 1
for N \leq 99: X = N = 2 * (N+1) - 11
for N \leq 999: X = N = 3 * (N+1) - 111
and so on…

So, for N \lt 10^x:
ans =x*(N+1) - \sum_{i = 0}^{x-1} 10^x

SOLUTIONS:

Setter's Solution
from sys import stdin
import math
for _ in range(int(stdin.readline())):
    n=int(stdin.readline())
    ans=0
    l=math.floor(math.log10(n)+1)
    for i in range(1,11):
        if(i==l):
            ans+=(n-10**(i-1)+1)*i
        elif(i<l):
            ans+=(9 * 10**(i - 1)) * i
        else:
            break
    print(ans*2)

Code Bitwise operator

Operators are updown should statement is right