Related Articles

Count total number of even sum sequences

• Difficulty Level : Hard
• Last Updated : 14 Jun, 2021

Given an integer N, the task is to count all possible sequences of length N such that all the elements of the sequence are from the range [1, N] and the sum of the elements of the sequence is even. As the answer could be very large so print the answer modulo 109 + 7.
Examples:

Input: N = 3
Output: 13
All possible sequences of length 3 will be (1, 1, 2), (1, 3, 2),
(3, 1, 2), (3, 3, 2), (1, 2, 1), (1, 2, 3), (3, 2, 1), (3, 2, 3),
(2, 1, 1), (2, 1, 3), (2, 3, 1), (2, 3, 3) and (2, 2, 2).
Input: N = 5
Output: 1562

Approach: To get even sum for any sequence, the number of odd elements must be even. Let’s choose to put x number of odd elements in the sequence where x is even. The total number of ways to put these odd numbers will be C(N, x) and in each position, y number of elements can be put where y is the count of odd numbers from 1 to N and the remaining positions can be filled with even numbers in the same way. So if x odd numbers are to be taken then their contribution will be C(N, x) * yx * (N – y)(N – x).
Below is the implementation of the above approach:

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 pll modInverse(ll n, ll p){    return power(n, p - 2, p);} // Function to return (nCr % p) using// Fermat's little theoremll 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 nll 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 nll counteEven(ll n){    ll x = n / 2;    return x;} // Function to return the count// of the required sequencesll 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 codeint main(){     ll n = 5;     cout << CountEvenSumSequences(n);     return 0;}

Java

 // Java implementation of the above approachimport 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 approachM = 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 pdef modInverse(n, p) :     return power(n, p - 2, p) # Function to return (nCr % p) using# Fermat's little theoremdef 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 ndef countOdd(n) :     x = n // 2    if (n % 2 == 1) :        x += 1    return x # Function to return the count of# even numbers from 1 to ndef counteEven(n) :     x = n // 2    return x # Function to return the count# of the required sequencesdef 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 coden = 5print(CountEvenSumSequences(n)) # This code is contributed by# divyamohan123

C#

 // C# implementation of the above approachusing 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


Output:

1562

Time Complexity: O(N*logN)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up