<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>

typedef long long ll;
ll expo(ll a, ll b, ll m){ll res = 1;while (b > 0){if (b & 1) res = (res * a) % m;a = (a * a) % m;b = b >> 1;}return res;}
ll mod_add(ll a, ll b, ll m){a = a % m;b = b % m;return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m){return ((a % m) * (b % m)) % m;}
ll mod_sub(ll a, ll b, ll m){a = a % m;b = b % m;return (((a - b) % m) + m) % m;}
ll mminvprime(ll a, ll m){return expo(a, m - 2, m);}
struct Hashing
{
  string s;
  int n;
  int primes;
  ll hashPrimes = 1000000009;
  const ll base = 29;
  vector<ll> hashValues;
  vector<ll> powersOfBase;
  vector<ll> inversePowersOfBase;
  Hashing(string a)
  {
    s = a;
    n = s.length();
    hashValues.resize(n);
    powersOfBase.resize(n+1);
    inversePowersOfBase.resize(n+1);
    powersOfBase[0] = 1;
    for (int j = 1; j <= n; j++)
    {
        powersOfBase[j] = (base * powersOfBase[j - 1]) % hashPrimes;
    }
    inversePowersOfBase[n] = mminvprime(powersOfBase[n], hashPrimes);
    for (int j = n - 1; j >= 0; j--)
    {
        inversePowersOfBase[j] = mod_mul(inversePowersOfBase[j + 1], base, hashPrimes);
    }
    for (int j = 0; j < n; j++)
    {
        hashValues[j] = ((s[j] - 'a' + 1LL) * powersOfBase[j]) % hashPrimes;
        hashValues[j] = (hashValues[j] + (j > 0 ? hashValues[j - 1] : 0LL)) % hashPrimes;
    }
  }
  ll substringHash(int l, int r)
  {
    ll val1 = hashValues[r];
    ll val2 = l > 0 ? hashValues[l - 1] : 0LL;
    ll hash = mod_mul(mod_sub(val1, val2, hashPrimes), inversePowersOfBase[l], hashPrimes);
    return hash;
  }
};
class Solution {
public:
    int repeatedStringMatch(string a, string b) {
        int n = a.size(), m = b.size();
        Hashing h2(b);
        string ans = "";
        int cnt = 0;
        while(ans.size()<m){
            ans += a;
            cnt++;
        }
        ll bval = h2.substringHash(0,m-1);
        for(int i=0;i<5;i++){
            int nn = ans.size();
            Hashing h1(ans);
            for(int j=0;j<=(nn-m);j++){
                if(bval==h1.substringHash(j,j+m-1)){
                    return cnt;
                }
            }
            ans += a;
            cnt++;
        }
        return -1;
    }
};
23869