📜  通过从1到N中选择三个数字来求和的方法数量

📅  最后修改于: 2021-06-25 15:45:05             🧑  作者: Mango

给定一个整数N,找到从{1,2,3…,N}中选择3个数字的方式数,使它们的和为偶数。
例子:

Input :  N = 3
Output : 1
Explanation: Select 1, 2 and 3

Input :  N = 4
Output :  2
Either select (1, 2, 3) or (1, 3, 4)

建议:在继续解决方案之前,请先在“实践”上解决它。

要求和,即使只有两种情况:

  1. 取2个奇数和1个偶数。
  2. 取所有偶数。
If n is even,
  Count of odd numbers = n/2 and even = n/2.
Else
  Count odd numbers = n/2 +1 and evne = n/2.

情况1 –方式数为:奇数C 2 *偶数。
情况2 –方式的数目将是:甚至是C 3
因此,总的方式将是Case_1_result + Case_2_result。

C++
// C++ program for above implementation
#include 
#define MOD 1000000007
using namespace std;
 
// Function to count number of ways
int countWays(int N)
{
    long long int count, odd = N / 2, even;
    if (N & 1)
        odd = N / 2 + 1;
 
    even = N / 2;
 
    // Case 1: 2 odds and 1 even
    count = (((odd * (odd - 1)) / 2) * even) % MOD;
 
    // Case 2: 3 evens
    count = (count + ((even * (even - 1) *
                           (even - 2)) / 6)) % MOD;
 
    return count;
}
 
// Driver code
int main()
{
    int n = 10;
    cout << countWays(n) << endl;
    return 0;
}


Java
// java program for above implementation
import java.io.*;
 
class GFG {
     
    static long MOD = 1000000007;
     
    // Function to count number of ways
    static long countWays(int N)
    {
        long count, odd = N / 2, even;
         
        if ((N & 1) > 0)
            odd = N / 2 + 1;
     
        even = N / 2;
     
        // Case 1: 2 odds and 1 even
        count = (((odd * (odd - 1)) / 2)
                          * even) % MOD;
     
        // Case 2: 3 evens
        count = (count + ((even * (even
                - 1) * (even - 2)) / 6))
                                  % MOD;
     
        return (long)count;
    }
     
    // Driver code
    static public void main (String[] args)
    {
        int n = 10;
         
        System.out.println(countWays(n));
    }
}
 
// This code is contributed by vt_m.


Python3
# Python3 code for above implementation
 
MOD = 1000000007
 
# Function to count number of ways
def countWays( N ):
    odd = N / 2
    if N & 1:
        odd = N / 2 + 1
    even = N / 2
     
    # Case 1: 2 odds and 1 even
    count = (((odd * (odd - 1)) / 2) * even) % MOD
 
    # Case 2: 3 evens
    count = (count + ((even * (even - 1) *
            (even - 2)) / 6)) % MOD
    return count
 
# Driver code
n = 10
print(int(countWays(n)))
 
# This code is contributed by "Sharad_Bhardwaj"


C#
// C# program for above implementation
using System;
 
public class GFG {
     
    static long MOD = 1000000007;
     
    // Function to count number of ways
    static long countWays(int N)
    {
        long count, odd = N / 2, even;
         
        if ((N & 1) > 0)
            odd = N / 2 + 1;
     
        even = N / 2;
     
        // Case 1: 2 odds and 1 even
        count = (((odd * (odd - 1)) / 2)
                            * even) % MOD;
     
        // Case 2: 3 evens
        count = (count + ((even * (even
                  - 1) * (even - 2)) / 6))
                                    % MOD;
     
        return (long)count;
    }
     
    // Driver code
    static public void Main ()
    {
        int n = 10;
 
        Console.WriteLine(countWays(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

60