📜  a + b + c = n的非负积分解的数量

📅  最后修改于: 2021-05-07 07:19:32             🧑  作者: Mango

给定数字n,请找到一些方法,我们可以将3个非负整数相加,使它们的总和为n。
例子 :

Input : n = 1
Output : 3
There are four ways to get sum 1.
(1, 0, 0), (0, 1, 0) and (0, 0, 1)

Input : n = 2
Output : 6
There are six ways to get sum 2.
(2, 0, 0), (0, 2, 0), (0, 0, 2), (1, 1, 0)
(1, 0, 1) and (0, 1, 1)

Input : n = 3
Output : 10
There are ten ways to get sum 10.
(3, 0, 0), (0, 3, 0), (0, 0, 3), (1, 2, 0)
(1, 0, 2), (0, 1, 2), (2, 1, 0), (2, 0, 1),
(0, 2, 1) and (1, 1, 1)

方法1 [蛮力:O(n 3 )]
一个简单的解决方案是使用三个循环来考虑所有三胞胎。对于每个三元组,检查其总和是否等于n。如果sum为n,则增加解决方案的计数。
下面是实现。

C++
// A naive C++ solution to count solutions of
// a + b + c = n
#include
using namespace std;
 
// Returns count of solutions of a + b + c = n
int countIntegralSolutions(int n)
{
    // Initialize result
    int result = 0;
 
    // Consider all triplets and increment
    // result whenever sum of a triplet is n.
    for (int i=0; i<=n; i++)
      for (int j=0; j<=n-i; j++)
          for (int k=0; k<=(n-i-j); k++)
             if (i + j + k == n)
              result++;
 
    return result;
}
 
// Driver code
int main()
{
    int n = 3;
    cout <<  countIntegralSolutions(n);
    return 0;
}


Java
// A naive Java solution to count
// solutions of a + b + c = n
import java.io.*;
 
class GFG
{
    // Returns count of solutions of a + b + c = n
    static int countIntegralSolutions(int n)
    {
        // Initialize result
        int result = 0;
     
        // Consider all triplets and increment
        // result whenever sum of a triplet is n.
        for (int i = 0; i <= n; i++)
        for (int j = 0; j <= n - i; j++)
            for (int k = 0; k <= (n - i - j); k++)
                if (i + j + k == n)
                result++;
     
        return result;
    }
 
    // Driver code
    public static void main (String[] args)
    {
        int n = 3;
        System.out.println( countIntegralSolutions(n));
     
    }
}
 
// This article is contributed by vt_m


Python3
# Python3 code to count
# solutions of a + b + c = n
 
# Returns count of solutions
# of a + b + c = n
def countIntegralSolutions (n):
 
    # Initialize result
    result = 0
     
    # Consider all triplets and
    # increment result whenever
    # sum of a triplet is n.
    for i in range(n + 1):
        for j in range(n + 1):
            for k in range(n + 1):
                if i + j + k == n:
                    result += 1
     
    return result
     
# Driver code
n = 3
print(countIntegralSolutions(n))
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// A naive C# solution to count
// solutions of a + b + c = n
using System;
 
class GFG {
     
    // Returns count of solutions
    // of a + b + c = n
    static int countIntegralSolutions(int n)
    {
         
        // Initialize result
        int result = 0;
     
        // Consider all triplets and increment
        // result whenever sum of a triplet is n.
        for (int i = 0; i <= n; i++)
            for (int j = 0; j <= n - i; j++)
                for (int k = 0; k <= (n - i - j); k++)
                    if (i + j + k == n)
                    result++;
     
        return result;
    }
 
    // Driver code
    public static void Main ()
    {
        int n = 3;
        Console.Write(countIntegralSolutions(n));
     
    }
}
 
// This article is contributed by Smitha.


PHP


Javascript


C++
// A naive C++ solution to count solutions of
// a + b + c = n
#include
using namespace std;
 
// Returns count of solutions of a + b + c = n
int countIntegralSolutions(int n)
{
    return ((n+1)*(n+2))/2;
}
 
// Driver code
int main()
{
    int n = 3;
    cout <<  countIntegralSolutions(n);
    return 0;
}


Java
// Java solution to count
// solutions of a + b + c = n
import java.io.*;
 
class GFG
{
    // Returns count of solutions
    // of a + b + c = n
    static int countIntegralSolutions(int n)
    {
    return ((n + 1) * (n + 2)) / 2;
         
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 3;
        System.out.println ( countIntegralSolutions(n));
         
    }
}
// This article is contributed by vt_m


Python3
# Python3 solution to count
# solutions of a + b + c = n
 
# Returns count of solutions
# of a + b + c = n
def countIntegralSolutions (n):
    return int(((n + 1) * (n + 2)) / 2)
     
# Driver code
n = 3
print(countIntegralSolutions(n))
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# solution to count
// solutions of a + b + c = n
using System;
 
class GFG {
     
    // Returns count of solutions
    // of a + b + c = n
    static int countIntegralSolutions(int n)
    {
        return ((n + 1) * (n + 2)) / 2;
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int n = 3;
        Console.Write ( countIntegralSolutions(n));
         
    }
}
 
// This code is contributed by parashar.


PHP


输出:

10

方法2 [直接公式:O(1)]
如果我们仔细研究一下模式,我们会发现解的数量为((n + 1)*(n + 2))/2。问题等同于分配n + 1个相同的球(对于0到0, n)在三个盒子中,解决方案是n + 2 C 2 。通常,如果有m个变量(或方框)和n个可能的值,则公式将变为n + m-1 C m-1

C++

// A naive C++ solution to count solutions of
// a + b + c = n
#include
using namespace std;
 
// Returns count of solutions of a + b + c = n
int countIntegralSolutions(int n)
{
    return ((n+1)*(n+2))/2;
}
 
// Driver code
int main()
{
    int n = 3;
    cout <<  countIntegralSolutions(n);
    return 0;
}

Java

// Java solution to count
// solutions of a + b + c = n
import java.io.*;
 
class GFG
{
    // Returns count of solutions
    // of a + b + c = n
    static int countIntegralSolutions(int n)
    {
    return ((n + 1) * (n + 2)) / 2;
         
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 3;
        System.out.println ( countIntegralSolutions(n));
         
    }
}
// This article is contributed by vt_m

Python3

# Python3 solution to count
# solutions of a + b + c = n
 
# Returns count of solutions
# of a + b + c = n
def countIntegralSolutions (n):
    return int(((n + 1) * (n + 2)) / 2)
     
# Driver code
n = 3
print(countIntegralSolutions(n))
 
# This code is contributed by "Sharad_Bhardwaj".

C#

// C# solution to count
// solutions of a + b + c = n
using System;
 
class GFG {
     
    // Returns count of solutions
    // of a + b + c = n
    static int countIntegralSolutions(int n)
    {
        return ((n + 1) * (n + 2)) / 2;
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int n = 3;
        Console.Write ( countIntegralSolutions(n));
         
    }
}
 
// This code is contributed by parashar.

的PHP


输出 :

10