Advertisement
DeepRest

Smallest Integer Divisible by K

Dec 30th, 2021
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. class Solution:
  2.     def smallestRepunitDivByK(self, k: int) -> int:
  3.         #since multiples of 2(evens) and 5 have their multiples divisible bt 2 and 5. Thus they cannot have 11..11 as their multiple
  4.         if k%2 == 0 or k%5 == 0:
  5.             return -1        
  6.        
  7.         res = 1
  8.         '''GP A: 1 10 10^2 10^3...(r=10)
  9.        N with i ones = sum of first i terms
  10.        GP B: 1 10%K (10%K)^2 (10%K)^3...(r=10%k)
  11.        (N with i ones)%k = (sum of first i terms)%k
  12.        '''
  13.         p, s, r = 1, 1, 10%k
  14.         #upper bound is k; because since there are only k distinct remainders so at most after k iterations(each iteration means one more 1) the remainders would repeat.
  15.         while s%k:
  16.             res += 1
  17.             p *= r #p is previous term in GP B(bi+1 = r*bi)
  18.             s += p%k # sum of first res terms of GP B
  19.  
  20.         return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement