How to calculate number of partitions of n?
<p>How can I calculate number of partitions of <code>n mod 1e9+7</code>, where <code>n&lt;=50000</code>.</p>

<p>See <a href="http://oeis.org/A000041" rel="nofollow">http://oeis.org/A000041</a> .</p>

<p>Here is the source problem <a href="http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1259" rel="nofollow">http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1259</a> (In Chinese)</p>

<p>Simply applying the formula: <code>a(n) = (1/n) * Sum_{k=0..n-1} d(n-k)*a(k)</code> gave me an <code>O(n^2)</code> solution.</p>

<pre><code>typedef long long ll;
ll MOD=1e9+7;
ll qp(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b&amp;1) ans=ans*a%MOD;
        a=a*a%MOD;
        b&gt;&gt;=1;
    }
    return ans;
}
ll a[50003],d[50003];
#define S 1000
int main()
{
    for(int i=1; i&lt;=S; i++)
    {
        for(int j=1; j&lt;=S; j++)
        {
            if(i%j==0) d[i]+=j;
        }
        d[i]%=MOD;
    }
    a[0]=1;
    for(int i=1; i&lt;=S; i++)
    {
        ll qwq=0;
        for(int j=0; j&lt;i; j++) qwq=qwq+d[i-j]*a[j]%MOD;
        qwq%=MOD;
        a[i]=qwq*qp(i,MOD-2)%MOD;
    }
    int n;
    cin&gt;&gt;n;
    cout&lt;&lt;a[n]&lt;&lt;"\n";
}
</code></pre>