📌  相关文章
📜  将 N 写成两个或多个正整数之和的方法 |组 2

📅  最后修改于: 2021-09-22 10:28:21             🧑  作者: Mango

给定一个数字 N,任务是找到 N 可以被划分的方式的数量,即 N 可以表示为正整数之和的方式的数量。
注意: N 本身也应被视为一种将其表示为正整数之和的方式。
例子:

这篇文章已经在将 n 写为两个或多个正整数之和的方法中讨论过。在这篇文章中,讨论了一种有效的方法。
方法(使用欧拉递归):
如果 p(n) 是 N 的分区数,则可以通过以下生成函数生成:
\sum_{n=0}^\infty p(n)x^n = \prod_{k=1}^\infty \left(\frac {1}{1-x^k} \right)
使用这个公式和欧拉五边形数定理,我们可以推导出 p(n) 的以下递推关系:(查看维基百科文章了解更多详情)
p(n) = p(n - 1) + p (n - 2) - p(n - 5) - p(n - 7) + \cdots + (-1)^{|{k} - 1|} \frac{k(3k - 1)}{2}
其中 k = 1, -1, 2, -2, 3, -3, … 并且 p(n) = 0 表示 n < 0。
下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to find the number
// of partitions of N
long long partitions(int n)
{
    vector p(n + 1, 0);
 
    // Base case
    p[0] = 1;
 
    for (int i = 1; i <= n; ++i) {
        int k = 1;
        while ((k * (3 * k - 1)) / 2 <= i) {
            p[i] += (k % 2 ? 1 : -1) * p[i - (k * (3 * k - 1)) / 2];
 
            if (k > 0)
                k *= -1;
            else
                k = 1 - k;
        }
    }
 
    return p[n];
}
 
// Driver code
int main()
{
    int N = 20;
    cout << partitions(N);
    return 0;
}


Java
// Java implementation of above approach
class GFG
{
 
    // Function to find the number
    // of partitions of N
    static long partitions(int n)
    {
        long p[] = new long[n + 1];
 
        // Base case
        p[0] = 1;
 
        for (int i = 1; i <= n; ++i)
        {
            int k = 1;
            while ((k * (3 * k - 1)) / 2 <= i)
            {
                p[i] += (k % 2 != 0 ? 1 : -1) *
                    p[i - (k * (3 * k - 1)) / 2];
 
                if (k > 0)
                {
                    k *= -1;
                }
                else
                {
                    k = 1 - k;
                }
            }
        }
        return p[n];
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int N = 20;
        System.out.println(partitions(N));
    }
}
 
// This code is contributed by Rajput-JI


Python 3
# Python 3 implementation of
# above approach
 
# Function to find the number
# of partitions of N
def partitions(n):
 
    p = [0] * (n + 1)
 
    # Base case
    p[0] = 1
 
    for i in range(1, n + 1):
        k = 1
        while ((k * (3 * k - 1)) / 2 <= i) :
            p[i] += ((1 if k % 2 else -1) *
                    p[i - (k * (3 * k - 1)) // 2])
 
            if (k > 0):
                k *= -1
            else:
                k = 1 - k
 
    return p[n]
 
# Driver code
if __name__ == "__main__":
    N = 20
    print(partitions(N))
 
# This code is contributed
# by ChitraNayal


C#
// C# implementation of above approach
using System;
 
class GFG
{
 
    // Function to find the number
    // of partitions of N
    static long partitions(int n)
    {
        long []p = new long[n + 1];
 
        // Base case
        p[0] = 1;
 
        for (int i = 1; i <= n; ++i)
        {
            int k = 1;
            while ((k * (3 * k - 1)) / 2 <= i)
            {
                p[i] += (k % 2 != 0 ? 1 : -1) *
                    p[i - (k * (3 * k - 1)) / 2];
 
                if (k > 0)
                {
                    k *= -1;
                }
                else
                {
                    k = 1 - k;
                }
            }
        }
        return p[n];
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int N = 20;
        Console.WriteLine(partitions(N));
    }
}
 
// This code has been contributed by 29AjayKumar


PHP
 0)
                $k *= -1;
            else
                $k = 1 - $k;
        }
    }
    return $p[$n];
}
 
// Driver Code
$N = 20;
print(partitions($N));
 
// This code is contributed
// by mits
?>


Javascript


输出:

627

时间复杂度:O(N√N)
空间复杂度:O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程