📜  计算偶数和序列的总数

📅  最后修改于: 2021-10-26 02:38:53             🧑  作者: Mango

给定一个整数N ,任务是对长度为N 的所有可能序列进行计数,使得该序列的所有元素都来自范围[1, N]并且该序列的元素之和为偶数。由于答案可能非常大,所以打印答案模10 9 + 7
例子:

方法:要获得任何序列的偶数和,奇数元素的数量必须是偶数。让我们选择把奇数元素的x个序列中,其中x是偶数。放置这些奇数的方法总数为C(N, x)并且在每个位置,可以放置y个元素,其中y是从1N的奇数个数,其余位置可以填充偶数以同样的方式。因此,如果要取x 个奇数,那么它们的贡献将是C(N, x) * y x * (N – y) (N – x)
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
#define M 1000000007
#define ll long long int
 
// Iterative function to calculate
// (x^y)%p in O(log y)
ll power(ll x, ll y, ll p)
{
 
    // Initialize result
    ll res = 1;
 
    // Update x if it is greater
    // than or equal to p
    x = x % p;
 
    while (y > 0) {
 
        // If y is odd then multiply
        // x with the result
        if (y & 1)
            res = (res * x) % p;
 
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
 
// Function to return n^(-1) mod p
ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}
 
// Function to return (nCr % p) using
// Fermat's little theorem
ll nCrModPFermat(ll n, ll r, ll p)
{
    // Base case
    if (r == 0)
        return 1;
 
    // Fill factorial array so that we
    // can find all factorial of r, n
    // and n-r
    ll fac[n + 1];
    fac[0] = 1;
    for (ll i = 1; i <= n; i++)
        fac[i] = fac[i - 1] * i % p;
 
    return (fac[n] * modInverse(fac[r], p) % p
            * modInverse(fac[n - r], p) % p)
           % p;
}
 
// Function to return the count of
// odd numbers from 1 to n
ll countOdd(ll n)
{
    ll x = n / 2;
    if (n % 2 == 1)
        x++;
    return x;
}
 
// Function to return the count of
// even numbers from 1 to n
ll counteEven(ll n)
{
    ll x = n / 2;
    return x;
}
 
// Function to return the count
// of the required sequences
ll CountEvenSumSequences(ll n)
{
 
    ll count = 0;
 
    for (ll i = 0; i <= n; i++) {
 
        // Take i even and n - i odd numbers
        ll even = i, odd = n - i;
 
        // Number of odd numbers must be even
        if (odd % 2 == 1)
            continue;
 
        // Total ways of placing n - i odd
        // numbers in the sequence of n numbers
        ll tot = (power(countOdd(n), odd, M)
                  * nCrModPFermat(n, odd, M))
                 % M;
        tot = (tot * power(counteEven(n), i, M)) % M;
 
        // Add this number to the final answer
        count += tot;
        count %= M;
    }
    return count;
}
 
// Driver code
int main()
{
 
    ll n = 5;
 
    cout << CountEvenSumSequences(n);
 
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
 
class GFG
{
    static final int M = 1000000007;
 
    // Iterative function to calculate
    // (x^y)%p in O(log y)
    static long power(long x, int y, int p)
    {
     
        // Initialize result
        long res = 1;
     
        // Update x if it is greater
        // than or equal to p
        x = x % p;
     
        while (y > 0)
        {
     
            // If y is odd then multiply
            // x with the result
            if ((y & 1) == 1)
                res = (res * x) % p;
     
            // y must be even now
            y = y >> 1; // y = y/2
            x = (x * x) % p;
        }
        return res;
    }
     
    // Function to return n^(-1) mod p
    static long modInverse(long n, int p)
    {
        return power(n, p - 2, p);
    }
     
    // Function to return (nCr % p) using
    // Fermat's little theorem
    static long nCrModPFermat(long n, int r, int p)
    {
        // Base case
        if (r == 0)
            return 1;
     
        // Fill factorial array so that we
        // can find all factorial of r, n
        // and n-r
        long fac[] = new long[(int)n + 1];
        fac[0] = 1;
        int i ;
        for ( i = 1; i <= n; i++)
            fac[i] = fac[i - 1] * i % p;
     
        return (fac[(int)n] * modInverse(fac[r], p) % p *
                              modInverse(fac[(int)n - r], p) % p) % p;
    }
     
    // Function to return the count of
    // odd numbers from 1 to n
    static long countOdd(long n)
    {
        long x = n / 2;
        if (n % 2 == 1)
            x++;
        return x;
    }
     
    // Function to return the count of
    // even numbers from 1 to n
    static long counteEven(long n)
    {
        long x = n / 2;
        return x;
    }
     
    // Function to return the count
    // of the required sequences
    static long CountEvenSumSequences(long n)
    {
        long count = 0;
     
        for (int i = 0; i <= n; i++)
        {
     
            // Take i even and n - i odd numbers
            int even = i, odd = (int)n - i;
     
            // Number of odd numbers must be even
            if (odd % 2 == 1)
                continue;
     
            // Total ways of placing n - i odd
            // numbers in the sequence of n numbers
            long tot = (power(countOdd(n), odd, M) *
                          nCrModPFermat(n, odd, M)) % M;
            tot = (tot * power(counteEven(n), i, M)) % M;
     
            // Add this number to the final answer
            count += tot;
            count %= M;
        }
        return count;
    }
     
    // Driver code
    public static void main (String[] args)
    {
     
        long n = 5;
     
        System.out.println(CountEvenSumSequences(n));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
M = 1000000007
 
# Iterative function to calculate
# (x^y)%p in O(log y)
def power(x, y, p):
 
    # Initialize result
    res = 1
 
    # Update x if it is greater
    # than or equal to p
    x = x % p
 
    while (y > 0) :
 
        # If y is odd then multiply
        # x with the result
        if (y & 1) :
            res = (res * x) % p
 
        # y must be even now
        y = y >> 1 # y = y/2
        x = (x * x) % p
         
    return res
 
# Function to return n^(-1) mod p
def modInverse(n, p) :
 
    return power(n, p - 2, p)
 
# Function to return (nCr % p) using
# Fermat's little theorem
def nCrModPFermat(n, r, p) :
     
    # Base case
    if (r == 0) :
        return 1
 
    # Fill factorial array so that we
    # can find all factorial of r, n
    # and n-r
    fac = [0] * (n+1)
    fac[0] = 1
    for i in range(1, n+1) :
        fac[i] = fac[i - 1] * i % p
 
    return (fac[n] * modInverse(fac[r], p) % p *
                     modInverse(fac[n - r], p) % p) % p
 
# Function to return the count of
# odd numbers from 1 to n
def countOdd(n) :
 
    x = n // 2
    if (n % 2 == 1) :
        x += 1
    return x
 
# Function to return the count of
# even numbers from 1 to n
def counteEven(n) :
 
    x = n // 2
    return x
 
# Function to return the count
# of the required sequences
def CountEvenSumSequences(n) :
 
    count = 0
 
    for i in range(n + 1) :
 
        # Take i even and n - i odd numbers
        even = i
        odd = n - i
 
        # Number of odd numbers must be even
        if (odd % 2 == 1) :
            continue
 
        # Total ways of placing n - i odd
        # numbers in the sequence of n numbers
        tot = (power(countOdd(n), odd, M) *
                 nCrModPFermat(n, odd, M)) % M
        tot = (tot * power(counteEven(n), i, M)) % M
 
        # Add this number to the final answer
        count += tot
        count %= M
     
    return count
 
# Driver code
n = 5
print(CountEvenSumSequences(n))
 
# This code is contributed by
# divyamohan123


C#
// C# implementation of the above approach
using System;        
 
class GFG
{
    static readonly int M = 1000000007;
 
    // Iterative function to calculate
    // (x^y)%p in O(log y)
    static long power(long x, int y, int p)
    {
     
        // Initialize result
        long res = 1;
     
        // Update x if it is greater
        // than or equal to p
        x = x % p;
     
        while (y > 0)
        {
     
            // If y is odd then multiply
            // x with the result
            if ((y & 1) == 1)
                res = (res * x) % p;
     
            // y must be even now
            y = y >> 1; // y = y/2
            x = (x * x) % p;
        }
        return res;
    }
     
    // Function to return n^(-1) mod p
    static long modInverse(long n, int p)
    {
        return power(n, p - 2, p);
    }
     
    // Function to return (nCr % p) using
    // Fermat's little theorem
    static long nCrModPFermat(long n, int r, int p)
    {
        // Base case
        if (r == 0)
            return 1;
     
        // Fill factorial array so that we
        // can find all factorial of r, n
        // and n-r
        long []fac = new long[(int)n + 1];
        fac[0] = 1;
        int i;
        for (i = 1; i <= n; i++)
            fac[i] = fac[i - 1] * i % p;
     
        return (fac[(int)n] * modInverse(fac[r], p) % p *
                              modInverse(fac[(int)n - r], p) % p) % p;
    }
     
    // Function to return the count of
    // odd numbers from 1 to n
    static long countOdd(long n)
    {
        long x = n / 2;
        if (n % 2 == 1)
            x++;
        return x;
    }
     
    // Function to return the count of
    // even numbers from 1 to n
    static long counteEven(long n)
    {
        long x = n / 2;
        return x;
    }
     
    // Function to return the count
    // of the required sequences
    static long CountEvenSumSequences(long n)
    {
        long count = 0;
     
        for (int i = 0; i <= n; i++)
        {
     
            // Take i even and n - i odd numbers
            int even = i, odd = (int)n - i;
     
            // Number of odd numbers must be even
            if (odd % 2 == 1)
                continue;
     
            // Total ways of placing n - i odd
            // numbers in the sequence of n numbers
            long tot = (power(countOdd(n), odd, M) *
                        nCrModPFermat(n, odd, M)) % M;
            tot = (tot * power(counteEven(n), i, M)) % M;
     
            // Add this number to the final answer
            count += tot;
            count %= M;
        }
        return count;
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        long n = 5;
     
        Console.WriteLine(CountEvenSumSequences(n));
    }
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:

1562

时间复杂度: O(N*logN)

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