Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def smallestRepunitDivByK(self, k: int) -> int:
- #since multiples of 2(evens) and 5 have their multiples divisible bt 2 and 5. Thus they cannot have 11..11 as their multiple
- if k%2 == 0 or k%5 == 0:
- return -1
- res = 1
- '''GP A: 1 10 10^2 10^3...(r=10)
- N with i ones = sum of first i terms
- GP B: 1 10%K (10%K)^2 (10%K)^3...(r=10%k)
- (N with i ones)%k = (sum of first i terms)%k
- '''
- p, s, r = 1, 1, 10%k
- #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.
- while s%k:
- res += 1
- p *= r #p is previous term in GP B(bi+1 = r*bi)
- s += p%k # sum of first res terms of GP B
- return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement