java 1ms modular solution with explanation
At first, if K ends with digit {0, 2, 4, 5, 6, 8}, i.e., K can be divided by 2 or 5, we can\'t find such integer.\nIf not, a intuitive idea is initiating the ta
At first, if ```K``` ends with digit {0, 2, 4, 5, 6, 8}, i.e., ```K``` can be divided by 2 or 5, we can\'t find such integer.\nIf not, a intuitive idea is initiating the target integer ```number``` to 1 and keep the operation ```number = number * 10 + 1``` until ```n``` can be divide by ```K```. However, if ```K``` is very large, ```number``` will overflow.\nHere, we need to make use of the characteristics of modular arithmetic:\n1. ```(a + b) % k = (a % k + b % k) % k```\n2. ```(a * b) % k = ( (a % k) * (b % k)) % k```\nAssume the smallest number is ```m = n * 10 + 1```, then ```(n * 10 + 1) % K = 0```, which means ```((n % K) * (10 % k) + 1 % K ) % K = 0```. Therefore, we can replace ```number``` with ```number % K``` to calculate the smallest number.\n```\npublic int smallestRepunitDivByK(int K) {\n\tif( K % 2 == 0 || K % 5 == 0 )\n\t\treturn -1;\n\tint base = 1, number = 1;\n\twhile( number % K != 0 ) {\n\t\tnumber = (number * 10 + 1) % K;\n\t\tbase++;\n\t}\n\treturn base;\n}\n```
['Java']