<p>Given two strings <code>a</code> and <code>b</code>, return <em>the minimum number of times you should repeat string </em><code>a</code><em> so that string</em> <code>b</code> <em>is a substring of it</em>. If it is impossible for <code>b</code>​​​​​​ to be a substring of <code>a</code> after repeating it, return <code>-1</code>.</p>

<p><strong>Notice:</strong> string <code>&quot;abc&quot;</code> repeated 0 times is <code>&quot;&quot;</code>, repeated 1 time is <code>&quot;abc&quot;</code> and repeated 2 times is <code>&quot;abcabc&quot;</code>.</p>

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

<pre>
<strong>Input:</strong> a = &quot;abcd&quot;, b = &quot;cdabcdab&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> We return 3 because by repeating a three times &quot;ab<strong>cdabcdab</strong>cd&quot;, b is a substring of it.
</pre>

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

<pre>
<strong>Input:</strong> a = &quot;a&quot;, b = &quot;aa&quot;
<strong>Output:</strong> 2
</pre>

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

<ul>
	<li><code>1 &lt;= a.length, b.length &lt;= 10<sup>4</sup></code></li>
	<li><code>a</code> and <code>b</code> consist of lowercase English letters.</li>
</ul>

class Solution {
public:
    #define ll long long int
    ll MOD = 1000000009;
    ll powerusingmod(ll a, ll b) {
        ll ans = 1;
        while (b != 0) {
            if (b % 2) {
                b = b - 1;
                ans = (ans * a) % MOD;
            } else {
                a = (a * a) % MOD;
                b /= 2;
            }
        }
        return ans % MOD;
    }
    ll hash(string& s,string &t) {
        ll n = s.size();
        ll nn = t.size();
        ll p = 31;
        ll m = 1e9 + 9;
        vector<ll> p_pow(max(n,nn)+5);
        vector<ll> p_inv_pow(max(n,nn)+5);
        p_pow[0] = 1;
        p_inv_pow[0] = 1;
        ll P_inv = powerusingmod(p, m - 2);
        ll tHash = 0;
        for (ll i = 1; i <= max(nn,n); i++) {
            p_pow[i] = (p_pow[i - 1] * p) % m;
            p_inv_pow[i] = (p_inv_pow[i - 1] * P_inv) % m;
        }
        vector<ll> h(n + 5, 0);
        for(ll i = 0; i < nn; ++i){
            tHash = (tHash + (t[i] - 'a' + 1) * p_pow[i]) % m;
        }cout << tHash << endl;
        for (ll i = 0; i < n; i++)
            h[i + 1] = (h[i] + (s[i] - 'a' + 1) * p_pow[i]) % m;
        // for(ll i = 0; i < n; ++i) cout << h[i+1] << endl;
        for(ll i = 0;i <= n-nn; ++i){
            ll rangeHash = (h[i+nn] + m - h[i]) % m;
            rangeHash = (rangeHash * p_inv_pow[i]) % m;
            cout << rangeHash << endl;
            if (rangeHash == tHash) return 0;
        }
        return -1;
    }
    int repeatedStringMatch(string a, string b) {
        string x=a;
        int cnt = 1;
        while(x.size()<b.size()){
            x += a;
            cnt ++;
        }
        if(hash(x,b) != -1) return cnt;
        x += a; cnt ++;
        if(hash(x,b) != -1) return cnt;
        return -1;
    }
};
14686