Algorithm 如何计算n的分区数?

Algorithm 如何计算n的分区数?,algorithm,math,series,Algorithm,Math,Series,如何计算n mod 1e9+7的分区数,其中n>=1; } 返回ans; } ll a[50003],d[50003]; #定义S 1000 int main() { 对于(inti=1;i我将用另一种方法求解它 动态规划: DP[N,K] = number of partitions of N using only numbers 1..K DP[0,k] = 1 DP[n,0] = 0 DP[n,k] when n<0 = 0 DP[n,k] when n>0 = DP[n-k,

如何计算
n mod 1e9+7
的分区数,其中
n>=1;
}
返回ans;
}
ll a[50003],d[50003];
#定义S 1000
int main()
{

对于(inti=1;i我将用另一种方法求解它

动态规划:

DP[N,K] = number of partitions of N using only numbers 1..K
DP[0,k] = 1
DP[n,0] = 0
DP[n,k] when n<0 = 0
DP[n,k] when n>0 = DP[n-k,k] + DP[n,k-1]
DP[N,K]=仅使用数字1..K的N个分区的数量
DP[0,k]=1
DP[n,0]=0
当n0=DP[n-k,k]+DP[n,k-1]时的DP[n,k]
使用记忆的递归实现:

ll partition(ll n, ll max){
    if (max == 0)
        return 0;
    if (n == 0)
        return 1;
    if (n < 0)
        return 0;

    if (memo[n][max] != 0)
        return memo[n][max];
    else
        return (memo[n][max] = (partition(n, max-1) + partition(n-max,max)));
}
ll分区(ll n,ll max){
如果(最大==0)
返回0;
如果(n==0)
返回1;
if(n<0)
返回0;
如果(备注[n][max]!=0)
返回备忘录[n][max];
其他的
返回(memo[n][max]=(分区(n,max-1)+分区(n-max,max));
}

我已经制作了一个O(n^2)解决方案。但是,既然如此,请向我们展示O(n^2)解决方案。您是否正在寻找所有的n≤50000,或者只是一些未知的n?而且,这听起来像是一个竞赛类型的问题。请告诉我们你从哪里得到这个问题,并发布问题的全文。@ZhongZiqian你的解决方案有效吗?请展示你的解决方案,以及你到底想改进什么。
ll partition(ll n, ll max){
    if (max == 0)
        return 0;
    if (n == 0)
        return 1;
    if (n < 0)
        return 0;

    if (memo[n][max] != 0)
        return memo[n][max];
    else
        return (memo[n][max] = (partition(n, max-1) + partition(n-max,max)));
}