Given Q queries. Each query contain a positive integer n. The task is to output the sum of sum of odd number digit contained in all the divisors of n.
Input : Q = 2, n1 = 10, n2 = 36
Output : 7 18
Divisors of 10 are 1, 2, 5, 10.
Sum of odd digits in 1 is 1, in 2 is 0, in 5 is 5, in 10 is 1.
So, sum became 7.
For Query 2,
Divisors of 36 are 1, 2, 3, 4, 6, 9, 12, 18, 36.
Sum of odd digits in 1 is 1, in 2 is 0, in 3 is 3, in 4 is 0,
in 6 is 0, in 9 is 9, in 12 is 1, in 18 is 1, in 36 is 3.
So, sum became 18.
The idea is to precompute the sum of odd number digit of all the numbers. Also, we can you use the sum of odd number digit of the previous number to compute the sum of odd number digit of the current number.
For example, to compute the sum of odd number digit of “123”, we can use the sum of odd number digit of “12” and “3”. Therefore, the sum of odd digit of “123” = sum of odd digit of “12” + add the last digit if it is odd (i.e 3).
Now, to find the sum of the sum of odd number digit of the factors, we can you use the jump phenomenon of Sieve of Eratosthenes. So, for all possible factors, add their contribution to its multiples.
For example, for 1 as the factor, add 1 (because 1 have only 1 odd digit) to all of its multiple.
for 2 as the factor, add 0 to all the multiples of 2 i.e 2, 4, 8, …
for 3 as the factor, add 1 to all the multiples of 3 i.e 3, 6, 9, …..
Below is the implementation of this approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Maximum Length of Sequence of Sums of prime factors generated by the given operations
- Maximum number of prime factors a number can have with exactly x factors
- Sum of M maximum distinct digit sum from 1 to N that are factors of K
- Find number of factors of N when location of its two factors whose product is N is given
- Check if a number exists having exactly N factors and K prime factors
- Sum of all odd factors of numbers in the range [l, r]
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Count of N-digit numbers having digit XOR as single digit
- Find sum of odd factors of a number
- Expressing a number as sum of consecutive | Set 2 (Using odd factors)
- Largest number less than N with digit sum greater than the digit sum of N
- Maximum and minimum sums from two numbers with digit replacements
- Minimum count of numbers required with unit digit X that sums up to N
- Count number of ordered pairs with Even and Odd Sums
- Queries to find whether a number has exactly four distinct factors or not
- Difference between sums of odd and even digits
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Number of elements with odd factors in given range
- Check whether count of odd and even factors of a number are equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.