<p>Given a positive integer <code>k</code>, you need to find the <strong>length</strong> of the <strong>smallest</strong> positive integer <code>n</code> such that <code>n</code> is divisible by <code>k</code>, and <code>n</code> only contains the digit <code>1</code>.</p>

<p>Return <em>the <strong>length</strong> of </em><code>n</code>. If there is no such <code>n</code>, return -1.</p>

<p><strong>Note:</strong> <code>n</code> may not fit in a 64-bit signed integer.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> k = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> The smallest answer is n = 1, which has length 1.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> k = 2
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no such positive integer n divisible by 2.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> k = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> The smallest answer is n = 111, which has length 3.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution {
public:
    int getNumWith1digit(int k){
        int ans=0;
        while(ans<k){
            ans= ans*10 + 1;
        }
        return ans;
    }
    int smallestRepunitDivByK(int k) {
        if (k%2==0 || k%5==0 || (k%10)%2==0 || k%10==0){
            return -1;
        }
        if(k==1){
            return 1;
        }
        unordered_set<int> vis;
        int num = getNumWith1digit(k);
        int ans=0, temp=num, rem=1;
        
        while(temp){
            ans++;
            temp=temp/10;
        }
        while(rem){
            rem = num%k;
            num=rem;
            if(rem==0){
                break;
            }
            while(num<k){
                num=num*10+1;
                ans++;
            }
            
            if(vis.find(rem) != vis.end()){
                return -1;
            }
            vis.insert(rem);
        }
        return ans;
    }
};
15800