<p>Given an integer n, return <em>the <strong>largest palindromic integer</strong> that can be represented as the product of two <code>n</code>-digits integers</em>. Since the answer can be very large, return it <strong>modulo</strong> <code>1337</code>.</p>

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

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 987
Explanation: 99 x 91 = 9009, 9009 % 1337 = 987
</pre>

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

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 9
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 8</code></li>
</ul>

#define ll long long
class Solution {
public:
    int largestPalindrome(int n) {
        if (n==1) return 9;
        ll base = pow(10,n);
        ll mini  = base/10;
        ll limit = base - mini;
        ll poli = base*base-1;
        vector<ll> d(n,9);    
        vector<ll> multi(n);  
        vector<ll> multi9(n); 
        int x = 1;
        for (ll x = 1,  i = n-1; i>=0; i--, x *=10) multi[i] = x;  
        for (ll x = 9,  i = n-1; i>=0; i--, x *=10) multi9[i] = x;  
        for( int i = 1; i<=limit; i++)
        {
            ll sq = sqrt((double)poli);
            for(ll k = base-1; k>sq; k--)
                if (poli%k == 0) return poli%1337;
            poli -= base;  
            int j=0;   
            while(d[j]==0)
            {
                poli += multi9[j];
                d[j] = 9;
                j++;
            }
            poli -= multi[j];
            d[j]--;
        }
        return -1;
    }
};
7800